cargo-grip4rust 0.6.0

A cargo subcommand for measuring Rust testability
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright 2026 Umberto Gotti <umberto.gotti@umbertogotti.dev>
// Licensed under the MIT License
// SPDX-License-Identifier: MIT

use std::collections::HashMap;
use std::path::Path;

use quote::ToTokens;
use syn::visit::Visit;
use syn::{Attribute, Item, ItemFn, Visibility};

use crate::contribution_schedule::ContributionSchedule;
use crate::function_info::FunctionInfo;
use crate::hidden_dep_finder::HiddenDepFinder;
use crate::io_call_finder::IoCallFinder;
use crate::item_counts::ItemCounts;
use crate::unsafe_finder::UnsafeFinder;

fn self_ty_name(ty: &syn::Type) -> String {
    if let syn::Type::Path(type_path) = ty {
        type_path
            .path
            .segments
            .first()
            .map(|s| s.ident.to_string())
            .unwrap_or_default()
    } else {
        String::new()
    }
}

fn is_trait_object_type(ty: &syn::Type) -> bool {
    match ty {
        syn::Type::TraitObject(_) => true,
        syn::Type::Reference(r) => is_trait_object_type(&r.elem),
        syn::Type::Path(p) => p.path.segments.iter().any(|seg| match &seg.arguments {
            syn::PathArguments::AngleBracketed(args) => args.args.iter().any(|arg| match arg {
                syn::GenericArgument::Type(t) => is_trait_object_type(t),
                _ => false,
            }),
            _ => false,
        }),
        _ => false,
    }
}

const KNOWN_FOREIGN_TRAITS: &[&str] = &[
    "Display",
    "Debug",
    "Clone",
    "Default",
    "PartialEq",
    "Eq",
    "PartialOrd",
    "Ord",
    "Hash",
    "Into",
    "From",
    "TryFrom",
    "Drop",
    "Deref",
    "DerefMut",
    "Index",
    "IndexMut",
    "Add",
    "Sub",
    "Mul",
    "Div",
    "Rem",
    "Neg",
    "Not",
    "Fn",
    "FnMut",
    "FnOnce",
    "Send",
    "Sync",
    "Sized",
    "ToString",
    "AsRef",
    "AsMut",
    "Borrow",
    "BorrowMut",
    "Error",
    "Read",
    "Write",
    "Seek",
    "BufRead",
    "Iterator",
    "IntoIterator",
    "Future",
    "IntoFuture",
    "Serialize",
    "Deserialize",
];

#[derive(Debug)]
pub struct Collector {
    counts: ItemCounts,
    functions: Vec<FunctionInfo>,
    current_file: String,
    struct_concrete_fields: HashMap<String, Vec<String>>,
    contribution_schedule: ContributionSchedule,
}

impl Collector {
    fn new(file: String) -> Self {
        Self {
            counts: ItemCounts::default(),
            functions: Vec::new(),
            current_file: file,
            struct_concrete_fields: HashMap::new(),
            contribution_schedule: ContributionSchedule::new(),
        }
    }

    pub fn collect(source: &str, path: &Path) -> (ItemCounts, Vec<FunctionInfo>) {
        let file = path.to_string_lossy().replace('\\', "/");
        let syntax = match syn::parse_file(source) {
            Ok(s) => s,
            Err(_) => return (ItemCounts::default(), Vec::new()),
        };

        let mut collector = Self::new(file);
        for item in &syntax.items {
            collector.visit_item(item);
        }
        (collector.counts, collector.functions)
    }
}

impl<'ast> Visit<'ast> for Collector {
    fn visit_item(&mut self, item: &'ast Item) {
        match item {
            Item::Fn(item_fn) if !self.has_test_attr(&item_fn.attrs) => self.visit_fn(item_fn),
            Item::Struct(item_struct) if !self.has_test_attr(&item_struct.attrs) => {
                self.visit_struct(item_struct);
            }
            Item::Trait(item_trait) if !self.has_test_attr(&item_trait.attrs) => {
                self.visit_trait(item_trait);
            }
            Item::Enum(item_enum) if !self.has_test_attr(&item_enum.attrs) => {
                self.visit_enum(item_enum);
            }
            Item::Mod(item_mod) if !self.has_test_attr(&item_mod.attrs) => self.visit_mod(item_mod),
            Item::Impl(item_impl) if !self.has_test_attr(&item_impl.attrs) => {
                self.visit_impl(item_impl);
            }
            _ => {}
        }
    }
}

impl Collector {
    fn visit_fn(&mut self, item_fn: &ItemFn) {
        let name = item_fn.sig.ident.to_string();
        let is_pub = matches!(
            self.classify_visibility(&item_fn.vis),
            VisibilityLevel::Pub | VisibilityLevel::PubCrate
        );
        let is_pure = self.is_probably_pure(item_fn);
        let finder = self.count_hidden_deps_in_block(&item_fn.block);
        let has_trait_seam = false;
        let contr = self
            .contribution_schedule
            .contribution(is_pure, has_trait_seam, finder.weight);

        self.counts.total_functions += 1;
        self.counts.total_items += 1;
        self.counts.total_contribution += contr;
        if contr == 1.0 {
            self.counts.clean_functions += 1;
        }
        if is_pub {
            self.counts.public_items += 1;
        }
        if is_pure {
            self.counts.pure_functions += 1;
        }

        self.functions.push(FunctionInfo {
            name,
            file: self.current_file.clone(),
            is_pure,
            is_public: is_pub,
            hidden_deps: finder.count,
            has_trait_seam,
            dep_weight: finder.weight,
            hidden_dep_labels: finder.labels,
            grip_absolute: contr,
            grip_normalized: (contr * 100.0).round() as u32,
        });
    }

    fn visit_struct(&mut self, item_struct: &syn::ItemStruct) {
        self.counts.total_items += 1;
        if matches!(
            self.classify_visibility(&item_struct.vis),
            VisibilityLevel::Pub | VisibilityLevel::PubCrate
        ) {
            self.counts.public_items += 1;
        }
        let name = item_struct.ident.to_string();
        let concrete: Vec<String> = item_struct
            .fields
            .iter()
            .filter(|f| !is_trait_object_type(&f.ty))
            .map(|f| f.ident.as_ref().map(|i| i.to_string()).unwrap_or_default())
            .filter(|n| !n.is_empty())
            .collect();
        self.struct_concrete_fields.insert(name, concrete);
    }

    fn visit_trait(&mut self, item_trait: &syn::ItemTrait) {
        self.counts.total_items += 1;
        if matches!(
            self.classify_visibility(&item_trait.vis),
            VisibilityLevel::Pub | VisibilityLevel::PubCrate
        ) {
            self.counts.public_items += 1;
        }
    }

    fn visit_enum(&mut self, item_enum: &syn::ItemEnum) {
        self.counts.total_items += 1;
        if matches!(
            self.classify_visibility(&item_enum.vis),
            VisibilityLevel::Pub | VisibilityLevel::PubCrate
        ) {
            self.counts.public_items += 1;
        }
    }

    fn visit_mod(&mut self, item_mod: &syn::ItemMod) {
        if let Some((_, items)) = &item_mod.content {
            for inner in items {
                self.visit_item(inner);
            }
        }
    }

    fn visit_impl(&mut self, item_impl: &syn::ItemImpl) {
        if self.is_foreign_trait_impl(item_impl) {
            return;
        }
        let is_trait_impl = item_impl.trait_.is_some();
        let self_ty_name = self::self_ty_name(&item_impl.self_ty);

        for item in &item_impl.items {
            if let syn::ImplItem::Fn(method) = item {
                if self.has_test_attr(&method.attrs) {
                    continue;
                }
                self.visit_impl_method(method, &self_ty_name, is_trait_impl);
            }
        }
    }

    fn is_foreign_trait_impl(&self, item_impl: &syn::ItemImpl) -> bool {
        item_impl
            .trait_
            .as_ref()
            .is_some_and(|(_, p, _)| self.is_foreign_trait(p))
    }

    fn visit_impl_method(
        &mut self,
        method: &syn::ImplItemFn,
        self_ty_name: &str,
        is_trait_impl: bool,
    ) {
        let is_pure = !self.is_impl_method_impure(method);
        let concrete_fields = self
            .struct_concrete_fields
            .get(self_ty_name)
            .cloned()
            .unwrap_or_default();
        let finder = self.count_hidden_deps_in_impl_method(method, concrete_fields);
        let contr = self
            .contribution_schedule
            .contribution(is_pure, is_trait_impl, finder.weight);

        self.counts.total_functions += 1;
        self.counts.total_contribution += contr;
        if contr == 1.0 {
            self.counts.clean_functions += 1;
        }
        if is_pure {
            self.counts.pure_functions += 1;
        }
        self.record_impl_method_kind(is_trait_impl, is_pure);

        let is_pub = matches!(
            self.classify_visibility(&method.vis),
            VisibilityLevel::Pub | VisibilityLevel::PubCrate
        );

        self.functions.push(FunctionInfo {
            name: method.sig.ident.to_string(),
            file: self.current_file.clone(),
            is_pure,
            is_public: is_pub,
            hidden_deps: finder.count,
            has_trait_seam: is_trait_impl,
            dep_weight: finder.weight,
            hidden_dep_labels: finder.labels,
            grip_absolute: contr,
            grip_normalized: (contr * 100.0).round() as u32,
        });
    }

    fn record_impl_method_kind(&mut self, is_trait_impl: bool, is_pure: bool) {
        if is_trait_impl {
            self.counts.local_trait_methods += 1;
            if !is_pure {
                self.counts.local_trait_impure += 1;
            }
        } else {
            self.counts.inherent_methods += 1;
            if !is_pure {
                self.counts.inherent_impure += 1;
            }
        }
    }

    fn is_impl_method_impure(&self, method: &syn::ImplItemFn) -> bool {
        if self.has_mut_param(&method.sig) {
            return true;
        }
        if self.is_unit_return(&method.sig) {
            return true;
        }
        if method.sig.unsafety.is_some() {
            return true;
        }
        self.has_unsafe_block(&method.block) || self.has_io_call(&method.block)
    }

    fn is_foreign_trait(&self, path: &syn::Path) -> bool {
        if let Some(last) = path.segments.last() {
            let name = last.ident.to_string();
            if KNOWN_FOREIGN_TRAITS.contains(&name.as_str()) {
                return true;
            }
        }
        if path.segments.len() > 1 {
            if let Some(first) = path.segments.first() {
                let name = first.ident.to_string();
                return name == "std" || name == "core" || name == "alloc";
            }
        }
        false
    }

    fn classify_visibility(&self, vis: &Visibility) -> VisibilityLevel {
        match vis {
            Visibility::Public(_) => VisibilityLevel::Pub,
            Visibility::Restricted(_) => VisibilityLevel::PubCrate,
            _ => VisibilityLevel::Private,
        }
    }

    fn has_test_attr(&self, attrs: &[Attribute]) -> bool {
        attrs.iter().any(|attr| {
            let tokens = attr.to_token_stream().to_string();
            let path = attr.path().get_ident().map(|i| i.to_string());
            matches!(path.as_deref(), Some("cfg")) && tokens.contains("test")
                || matches!(path.as_deref(), Some("test"))
                || matches!(path.as_deref(), Some("cfg_attr")) && tokens.contains("test")
        })
    }

    fn is_probably_pure(&self, item_fn: &ItemFn) -> bool {
        if self.has_mut_param(&item_fn.sig) {
            return false;
        }
        if self.is_unit_return(&item_fn.sig) {
            return false;
        }
        if item_fn.sig.unsafety.is_some() {
            return false;
        }
        !self.has_unsafe_block(&item_fn.block)
    }

    fn has_mut_param(&self, sig: &syn::Signature) -> bool {
        sig.inputs.iter().any(|arg| match arg {
            syn::FnArg::Receiver(recv) => recv.mutability.is_some(),
            syn::FnArg::Typed(pat_type) => self.has_mut_in_type(&pat_type.ty),
        })
    }

    #[allow(clippy::only_used_in_recursion)]
    fn has_mut_in_type(&self, ty: &syn::Type) -> bool {
        use syn::Type;
        match ty {
            Type::Reference(reference) => reference.mutability.is_some(),
            Type::Paren(inner) => self.has_mut_in_type(&inner.elem),
            _ => false,
        }
    }

    fn is_unit_return(&self, sig: &syn::Signature) -> bool {
        match &sig.output {
            syn::ReturnType::Default => true,
            syn::ReturnType::Type(_, ty) => {
                if let syn::Type::Tuple(tuple) = ty.as_ref() {
                    tuple.elems.is_empty()
                } else {
                    false
                }
            }
        }
    }

    fn has_unsafe_block(&self, block: &syn::Block) -> bool {
        let mut finder = UnsafeFinder::new();
        finder.visit_block(block);
        finder.found
    }

    fn has_io_call(&self, block: &syn::Block) -> bool {
        let mut finder = IoCallFinder::new();
        finder.visit_block(block);
        finder.found
    }

    fn count_hidden_deps_in_block(&self, block: &syn::Block) -> HiddenDepFinder {
        let mut finder = HiddenDepFinder::new();
        finder.visit_block(block);
        finder
    }

    fn count_hidden_deps_in_impl_method(
        &self,
        method: &syn::ImplItemFn,
        concrete_fields: Vec<String>,
    ) -> HiddenDepFinder {
        let mut finder = HiddenDepFinder::new();
        finder.set_concrete_fields(concrete_fields);
        finder.visit_block(&method.block);
        finder
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum VisibilityLevel {
    Private,
    PubCrate,
    Pub,
}