• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

xd009642 / tarpaulin / #751

25 Apr 2026 06:52AM UTC coverage: 85.762% (+0.2%) from 85.516%
#751

push

xd009642
Merge branch 'develop'

179 of 210 new or added lines in 7 files covered. (85.24%)

6 existing lines in 3 files now uncovered.

4909 of 5724 relevant lines covered (85.76%)

249119.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.27
/src/source_analysis/items.rs
1
use crate::source_analysis::prelude::*;
2
use syn::*;
3

4
impl SourceAnalysis {
5
    pub(crate) fn process_items(&mut self, items: &[Item], ctx: &Context) -> SubResult {
588✔
6
        let mut res = SubResult::Ok;
1,176✔
7
        for item in items.iter() {
2,232✔
8
            match item {
1,056✔
9
                Item::ExternCrate(i) => {
4✔
10
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
12✔
11
                    analysis.ignore_tokens(i);
4✔
12
                }
13
                Item::Use(i) => {
192✔
14
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
576✔
15
                    analysis.ignore_tokens(i);
192✔
16
                }
17
                Item::Mod(i) => self.visit_mod(i, ctx),
940✔
18
                Item::Fn(i) => self.visit_fn(i, ctx, false),
3,210✔
19
                Item::Struct(i) => {
84✔
20
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
252✔
21
                    analysis.ignore_tokens(i);
84✔
22
                }
23
                Item::Enum(i) => {
12✔
24
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
36✔
25
                    analysis.ignore_tokens(i);
12✔
26
                }
27
                Item::Union(i) => {
4✔
28
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
12✔
29
                    analysis.ignore_tokens(i);
4✔
30
                }
31
                Item::Trait(i) => self.visit_trait(i, ctx),
110✔
32
                Item::Impl(i) => self.visit_impl(i, ctx),
180✔
33
                Item::Macro(ref i) => {
×
34
                    if self.visit_macro_call(&i.mac, ctx).is_unreachable() {
×
35
                        res = SubResult::Unreachable;
×
36
                    }
37
                }
38
                Item::Const(c) => {
40✔
39
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
120✔
40
                    analysis.ignore_tokens(c);
40✔
41
                }
42
                _ => {}
×
43
            }
44
        }
45
        res
588✔
46
    }
47

48
    fn ignore_nested_modules(&mut self, items: &[Item], ctx: &Context) {
24✔
49
        for item in items.iter() {
86✔
50
            if let Item::Mod(m) = item {
46✔
51
                if let Some((_, ref items)) = m.content {
12✔
52
                    let _guard = ctx.push_to_symbol_stack(m.ident.to_string());
12✔
53
                    self.ignore_nested_modules(items, ctx);
6✔
54
                } else {
55
                    // ignore whole ass module
56
                    let mut p = if let Some(parent) = ctx.file.parent() {
18✔
57
                        parent.to_path_buf()
12✔
58
                    } else {
59
                        PathBuf::new()
×
60
                    };
61
                    match ctx.file.file_stem().and_then(|x| x.to_str()) {
30✔
62
                        Some(name) if !["lib", "mod"].contains(&name) => {
24✔
63
                            p.push(name);
4✔
64
                        }
65
                        _ => {}
4✔
66
                    }
67
                    for s in ctx.symbol_stack.borrow().iter() {
22✔
68
                        p.push(s);
16✔
69
                    }
70
                    p.push(m.ident.to_string());
24✔
71
                    if !p.exists() {
12✔
72
                        p.set_extension("rs");
6✔
73
                    }
74
                    ctx.ignore_mods.borrow_mut().insert(p);
12✔
75
                }
76
            }
77
        }
78
    }
79

80
    fn visit_mod(&mut self, module: &ItemMod, ctx: &Context) {
188✔
81
        let _guard = ctx.push_to_symbol_stack(module.ident.to_string());
940✔
82
        let analysis = self.get_line_analysis(ctx.file.to_path_buf());
940✔
83
        analysis.ignore_tokens(module.mod_token);
564✔
84
        let should_cover = self.check_attr_list(&module.attrs, ctx);
940✔
85
        if should_cover {
188✔
86
            if let Some((_, ref items)) = module.content {
342✔
87
                self.process_items(items, ctx);
276✔
88
            }
89
        } else {
90
            if let Some((ref braces, _)) = module.content {
52✔
91
                let analysis = self.get_line_analysis(ctx.file.to_path_buf());
110✔
92
                analysis.ignore_span(braces.span.join());
88✔
93
                if let Some((_, ref items)) = module.content {
66✔
94
                    self.ignore_nested_modules(items, ctx);
66✔
95
                }
96
            } else {
97
                // This item was determined not to be covered, so we ignore it.
98
                // Do a best-effort calculation of the path of the module
99
                // corresponding to the mod item to ignore it.
100

101
                // Get the file or directory name of the module containing the mod item
102
                let mut p = if let Some(parent) = ctx.file.parent() {
24✔
103
                    parent.to_path_buf()
16✔
104
                } else {
105
                    PathBuf::new()
×
106
                };
107
                match ctx.file.file_stem().and_then(|x| x.to_str()) {
40✔
108
                    Some(name) if ["lib", "mod", "main"].contains(&name) => {
42✔
109
                        // skip because these segments are transparent in module tree
110
                    }
111
                    Some(name) => p.push(name),
8✔
NEW
112
                    None => warn!(
×
113
                        "Could not read file stem of {:?}; sub-module path may be wrong",
114
                        ctx.file
115
                    ),
116
                }
117
                let stack = ctx.symbol_stack.borrow();
24✔
118
                for s in stack.iter().take(stack.len() - 1) {
28✔
119
                    p.push(s);
4✔
120
                }
121
                p.push(module.ident.to_string());
32✔
122
                if !p.exists() {
16✔
123
                    p.set_extension("rs");
8✔
124
                }
125
                ctx.ignore_mods.borrow_mut().insert(p);
16✔
126
            }
127
        }
128
    }
129

130
    fn visit_fn(&mut self, func: &ItemFn, ctx: &Context, force_cover: bool) {
690✔
131
        let _guard = ctx.push_to_symbol_stack(func.sig.ident.to_string());
3,450✔
132
        {
133
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
4,140✔
134
            let span = func.span();
2,760✔
135
            analysis.functions.insert(
2,070✔
136
                ctx.get_qualified_name(),
2,070✔
137
                (func.sig.span().start().line, span.end().line),
1,380✔
138
            );
139
        }
140
        let mut test_func = false;
1,380✔
141
        let mut ignored_attr = false;
1,380✔
142
        let mut is_inline = false;
1,380✔
143
        let mut ignore_span = false;
1,380✔
144
        let is_generic = is_sig_generic(&func.sig);
2,070✔
145
        for attr in &func.attrs {
1,242✔
146
            let id = attr.path();
1,656✔
147
            if id.is_ident("test") || id.segments.last().is_some_and(|seg| seg.ident == "test") {
3,490✔
148
                test_func = true;
244✔
149
            } else if id.is_ident("derive") {
1,168✔
150
                let analysis = self.get_line_analysis(ctx.file.to_path_buf());
×
151
                analysis.ignore_span(attr.span());
×
152
            } else if id.is_ident("inline") {
928✔
153
                is_inline = true;
4✔
154
            } else if id.is_ident("ignore") {
916✔
155
                ignored_attr = true;
×
156
            } else if check_cfg_attr(&attr.meta) {
608✔
157
                ignore_span = true;
24✔
158
                break;
24✔
159
            }
160
        }
161
        if ignore_span
690✔
162
            || (test_func && !ctx.config.include_tests())
906✔
163
            || (ignored_attr && !ctx.config.run_ignored)
650✔
164
        {
165
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
240✔
166
            analysis.ignore_tokens(func);
80✔
167
        } else {
168
            if is_inline || is_generic || force_cover {
1,968✔
169
                let analysis = self.get_line_analysis(ctx.file.to_path_buf());
456✔
170
                // We need to force cover!
171
                analysis.cover_span(func.block.span(), Some(ctx.file_contents));
304✔
172
            }
173
            if self
1,300✔
174
                .process_statements(&func.block.stmts, ctx)
1,300✔
175
                .is_unreachable()
176
            {
177
                // if the whole body of the function is unreachable, that means the function itself
178
                // cannot be called, so is unreachable as a whole
179
                let analysis = self.get_line_analysis(ctx.file.to_path_buf());
50✔
180
                analysis.ignore_tokens(func);
30✔
181
                return;
10✔
182
            }
183
            self.visit_generics(&func.sig.generics, ctx);
2,560✔
184
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
3,200✔
185
            let line_number = func.sig.fn_token.span().start().line;
1,280✔
186
            let mut start_line = line_number;
1,280✔
187
            for attr in &func.attrs {
1,656✔
188
                start_line = start_line.min(attr.span().start().line);
1,524✔
189
            }
190
            if start_line < line_number {
940✔
191
                analysis.add_to_ignore(start_line..line_number);
900✔
192
            }
193
            analysis.ignore.remove(&Lines::Line(line_number));
1,920✔
194
            // Ignore multiple lines of fn decl
195
            let decl_start = func.sig.fn_token.span().start().line + 1;
1,280✔
196
            let stmts_start = func.block.span().start().line;
1,280✔
197
            let lines = decl_start..=stmts_start;
1,280✔
198
            analysis.add_to_ignore(lines);
1,920✔
199
        }
200
    }
201

202
    fn visit_trait(&mut self, trait_item: &ItemTrait, ctx: &Context) {
22✔
203
        let _guard = ctx.push_to_symbol_stack(trait_item.ident.to_string());
110✔
204
        let check_cover = self.check_attr_list(&trait_item.attrs, ctx);
110✔
205
        if check_cover {
22✔
206
            for item in &trait_item.items {
44✔
207
                if let TraitItem::Fn(ref i) = *item {
46✔
208
                    if self.check_attr_list(&i.attrs, ctx) {
88✔
209
                        let item = i.clone();
54✔
210
                        if let Some(block) = item.default {
46✔
211
                            let item_fn = ItemFn {
212
                                attrs: item.attrs,
42✔
213
                                // Trait functions inherit visibility from the trait
214
                                vis: trait_item.vis.clone(),
56✔
215
                                sig: item.sig,
42✔
216
                                block: Box::new(block),
28✔
217
                            };
218
                            // We visit the function and force cover it
219
                            self.visit_fn(&item_fn, ctx, true);
42✔
220
                        } else {
221
                            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
24✔
222
                            analysis.ignore_tokens(i);
8✔
223
                        }
224
                    } else {
225
                        let analysis = self.get_line_analysis(ctx.file.to_path_buf());
24✔
226
                        analysis.ignore_tokens(i);
8✔
227
                    }
228
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
110✔
229
                    for a in &i.attrs {
34✔
230
                        analysis.ignore_tokens(a);
12✔
231
                    }
232
                }
233
            }
234
            self.visit_generics(&trait_item.generics, ctx);
80✔
235
        } else {
236
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
12✔
237
            analysis.ignore_tokens(trait_item);
4✔
238
        }
239
    }
240

241
    fn visit_impl(&mut self, impl_blk: &ItemImpl, ctx: &Context) {
36✔
242
        let self_ty_name = impl_blk
108✔
243
            .self_ty
72✔
244
            .to_token_stream()
245
            .to_string()
246
            .replace(' ', "");
247
        let _guard = match &impl_blk.trait_ {
72✔
248
            Some((_, path, _)) => {
12✔
249
                let trait_name = path
24✔
250
                    .segments
12✔
251
                    .last()
252
                    .map(|x| x.ident.to_string())
36✔
253
                    .unwrap_or_else(|| path.to_token_stream().to_string());
12✔
254
                let name = format!("<impl {} for {}>", trait_name, self_ty_name);
24✔
255
                ctx.push_to_symbol_stack(name)
36✔
256
            }
257
            None => ctx.push_to_symbol_stack(self_ty_name),
72✔
258
        };
259
        let check_cover = self.check_attr_list(&impl_blk.attrs, ctx);
180✔
260
        if check_cover {
36✔
261
            for item in &impl_blk.items {
70✔
262
                match *item {
36✔
263
                    ImplItem::Fn(ref i) => {
68✔
264
                        let item = i.clone();
136✔
265
                        let item_fn = ItemFn {
266
                            attrs: item.attrs,
102✔
267
                            vis: item.vis,
102✔
268
                            sig: item.sig,
102✔
269
                            block: Box::new(item.block),
68✔
270
                        };
271

272
                        // If the impl is on a generic, we need to force cover
273
                        let force_cover = !impl_blk.generics.params.is_empty();
102✔
274

275
                        self.visit_fn(&item_fn, ctx, force_cover);
136✔
276
                    }
277
                    ImplItem::Type(_) => {
2✔
278
                        let analysis = self.get_line_analysis(ctx.file.to_path_buf());
12✔
279
                        analysis.ignore_span(item.span());
6✔
280
                    }
281
                    _ => {}
×
282
                }
283
            }
284
            self.visit_generics(&impl_blk.generics, ctx);
136✔
285
        } else {
286
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
12✔
287
            analysis.ignore_tokens(impl_blk);
4✔
288
        }
289
    }
290
}
291

292
fn has_generic_arg<'a>(args: impl Iterator<Item = &'a FnArg>) -> bool {
642✔
293
    for arg in args {
862✔
294
        if let FnArg::Typed(pat) = arg {
426✔
295
            if matches!(*pat.ty, Type::ImplTrait(_)) {
410✔
296
                return true;
2✔
297
            }
298
        }
299
    }
300
    false
640✔
301
}
302

303
fn is_sig_generic(sig: &Signature) -> bool {
690✔
304
    !sig.generics.params.is_empty() || has_generic_arg(sig.inputs.iter())
2,616✔
305
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc