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

xd009642 / tarpaulin / #502

31 May 2024 06:52PM UTC coverage: 75.153% (+0.5%) from 74.697%
#502

Pull #1563

xd009642
Put function names into lcov report
Pull Request #1563: Start revamping the function handling stuff

58 of 62 new or added lines in 6 files covered. (93.55%)

59 existing lines in 4 files now uncovered.

2583 of 3437 relevant lines covered (75.15%)

136538.58 hits per line

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

93.1
/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 {
379✔
6
        let mut res = SubResult::Ok;
379✔
7
        for item in items.iter() {
1,067✔
8
            match item {
688✔
9
                Item::ExternCrate(i) => {
2✔
10
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
2✔
11
                    analysis.ignore_tokens(i);
2✔
12
                }
13
                Item::Use(i) => {
47✔
14
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
47✔
15
                    analysis.ignore_tokens(i);
47✔
16
                }
17
                Item::Mod(i) => self.visit_mod(i, ctx),
140✔
18
                Item::Fn(i) => self.visit_fn(i, ctx, false),
442✔
19
                Item::Struct(i) => {
23✔
20
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
23✔
21
                    analysis.ignore_tokens(i);
23✔
22
                }
23
                Item::Enum(i) => {
4✔
24
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
4✔
25
                    analysis.ignore_tokens(i);
4✔
26
                }
27
                Item::Union(i) => {
1✔
28
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
1✔
29
                    analysis.ignore_tokens(i);
1✔
30
                }
31
                Item::Trait(i) => self.visit_trait(i, ctx),
9✔
32
                Item::Impl(i) => self.visit_impl(i, ctx),
14✔
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) => {
6✔
39
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
6✔
40
                    analysis.ignore_tokens(c);
6✔
41
                }
42
                _ => {}
×
43
            }
44
        }
45
        res
379✔
46
    }
47

48
    fn visit_mod(&mut self, module: &ItemMod, ctx: &Context) {
140✔
49
        let _guard = ctx.push_to_symbol_stack(module.ident.to_string());
140✔
50
        let analysis = self.get_line_analysis(ctx.file.to_path_buf());
140✔
51
        analysis.ignore_tokens(module.mod_token);
140✔
52
        let check_insides = self.check_attr_list(&module.attrs, ctx);
140✔
53
        if check_insides {
140✔
54
            if let Some((_, ref items)) = module.content {
161✔
55
                self.process_items(items, ctx);
56
            }
57
        } else {
58
            if let Some((ref braces, _)) = module.content {
70✔
59
                let analysis = self.get_line_analysis(ctx.file.to_path_buf());
60
                analysis.ignore_span(braces.span.join());
61
            }
62
            // Get the file or directory name of the module
63
            let mut p = if let Some(parent) = ctx.file.parent() {
32✔
64
                parent.join(module.ident.to_string())
65
            } else {
66
                PathBuf::from(module.ident.to_string())
3✔
67
            };
68
            if !p.exists() {
33✔
69
                p.set_extension("rs");
33✔
70
            }
71
            ctx.ignore_mods.borrow_mut().insert(p);
35✔
72
        }
73
    }
74

75
    fn visit_fn(&mut self, func: &ItemFn, ctx: &Context, force_cover: bool) {
461✔
76
        let _guard = ctx.push_to_symbol_stack(func.sig.ident.to_string());
461✔
77
        {
78
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
461✔
79
            let span = func.span();
461✔
80
            analysis.functions.insert(
461✔
81
                ctx.get_qualified_name(),
461✔
82
                (span.start().line, span.end().line),
461✔
83
            );
84
        }
85
        let mut test_func = false;
461✔
86
        let mut ignored_attr = false;
461✔
87
        let mut is_inline = false;
461✔
88
        let mut ignore_span = false;
461✔
89
        let is_generic = is_sig_generic(&func.sig);
461✔
90
        for attr in &func.attrs {
1,282✔
91
            let id = attr.path();
92
            if id.is_ident("test") || id.segments.last().is_some_and(|seg| seg.ident == "test") {
663✔
93
                test_func = true;
181✔
94
            } else if id.is_ident("derive") {
417✔
95
                let analysis = self.get_line_analysis(ctx.file.to_path_buf());
×
96
                analysis.ignore_span(attr.span());
×
97
            } else if id.is_ident("inline") {
237✔
98
                is_inline = true;
1✔
99
            } else if id.is_ident("ignore") {
236✔
100
                ignored_attr = true;
×
101
            } else if check_cfg_attr(&attr.meta) {
235✔
102
                ignore_span = true;
13✔
103
                break;
13✔
104
            }
105
        }
106
        if ignore_span
461✔
107
            || (test_func && !ctx.config.include_tests())
625✔
108
            || (ignored_attr && !ctx.config.run_ignored)
419✔
109
        {
110
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
42✔
111
            analysis.ignore_tokens(func);
42✔
112
        } else {
113
            if is_inline || is_generic || force_cover {
1,263✔
114
                let analysis = self.get_line_analysis(ctx.file.to_path_buf());
17✔
115
                // We need to force cover!
116
                analysis.cover_span(func.block.span(), Some(ctx.file_contents));
17✔
117
            }
118
            if self
419✔
119
                .process_statements(&func.block.stmts, ctx)
419✔
120
                .is_unreachable()
121
            {
122
                // if the whole body of the function is unreachable, that means the function itself
123
                // cannot be called, so is unreachable as a whole
124
                let analysis = self.get_line_analysis(ctx.file.to_path_buf());
5✔
125
                analysis.ignore_tokens(func);
5✔
126
                return;
5✔
127
            }
128
            self.visit_generics(&func.sig.generics, ctx);
414✔
129
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
414✔
130
            let line_number = func.sig.fn_token.span().start().line;
414✔
131
            let mut start_line = line_number;
414✔
132
            for attr in &func.attrs {
1,491✔
133
                start_line = start_line.min(attr.span().start().line);
359✔
134
            }
135
            if start_line < line_number {
207✔
136
                analysis.add_to_ignore(start_line..line_number);
207✔
137
            }
138
            analysis.ignore.remove(&Lines::Line(line_number));
414✔
139
            // Ignore multiple lines of fn decl
140
            let decl_start = func.sig.fn_token.span().start().line + 1;
414✔
141
            let stmts_start = func.block.span().start().line;
414✔
142
            let lines = decl_start..=stmts_start;
414✔
143
            analysis.add_to_ignore(lines);
414✔
144
        }
145
    }
146

147
    fn visit_trait(&mut self, trait_item: &ItemTrait, ctx: &Context) {
9✔
148
        let check_cover = self.check_attr_list(&trait_item.attrs, ctx);
9✔
149
        if check_cover {
9✔
150
            for item in &trait_item.items {
26✔
151
                if let TraitItem::Fn(ref i) = *item {
8✔
152
                    if self.check_attr_list(&i.attrs, ctx) {
153
                        let item = i.clone();
6✔
154
                        if let Some(block) = item.default {
18✔
155
                            let item_fn = ItemFn {
156
                                attrs: item.attrs,
6✔
157
                                // Trait functions inherit visibility from the trait
158
                                vis: trait_item.vis.clone(),
6✔
159
                                sig: item.sig,
6✔
160
                                block: Box::new(block),
6✔
161
                            };
162
                            // We visit the function and force cover it
163
                            self.visit_fn(&item_fn, ctx, true);
6✔
164
                        } else {
UNCOV
165
                            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
×
166
                            analysis.ignore_tokens(i);
×
167
                        }
168
                    } else {
169
                        let analysis = self.get_line_analysis(ctx.file.to_path_buf());
2✔
170
                        analysis.ignore_tokens(i);
2✔
171
                    }
172
                    let analysis = self.get_line_analysis(ctx.file.to_path_buf());
8✔
173
                    for a in &i.attrs {
17✔
174
                        analysis.ignore_tokens(a);
3✔
175
                    }
176
                }
177
            }
178
            self.visit_generics(&trait_item.generics, ctx);
8✔
179
        } else {
180
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
1✔
181
            analysis.ignore_tokens(trait_item);
1✔
182
        }
183
    }
184

185
    fn visit_impl(&mut self, impl_blk: &ItemImpl, ctx: &Context) {
14✔
186
        let _guard = ctx.push_to_symbol_stack(impl_blk.self_ty.to_token_stream().to_string());
14✔
187
        let check_cover = self.check_attr_list(&impl_blk.attrs, ctx);
14✔
188
        if check_cover {
14✔
189
            for item in &impl_blk.items {
41✔
190
                match *item {
191
                    ImplItem::Fn(ref i) => {
13✔
192
                        let item = i.clone();
13✔
193
                        let item_fn = ItemFn {
194
                            attrs: item.attrs,
13✔
195
                            vis: item.vis,
13✔
196
                            sig: item.sig,
13✔
197
                            block: Box::new(item.block),
13✔
198
                        };
199

200
                        // If the impl is on a generic, we need to force cover
201
                        let force_cover = !impl_blk.generics.params.is_empty();
13✔
202

203
                        self.visit_fn(&item_fn, ctx, force_cover);
13✔
204
                    }
205
                    ImplItem::Type(_) => {
1✔
206
                        let analysis = self.get_line_analysis(ctx.file.to_path_buf());
1✔
207
                        analysis.ignore_span(item.span());
1✔
208
                    }
UNCOV
209
                    _ => {}
×
210
                }
211
            }
212
            self.visit_generics(&impl_blk.generics, ctx);
13✔
213
        } else {
214
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
1✔
215
            analysis.ignore_tokens(impl_blk);
1✔
216
        }
217
    }
218
}
219

220
fn has_generic_arg<'a>(args: impl Iterator<Item = &'a FnArg>) -> bool {
453✔
221
    for arg in args {
796✔
222
        if let FnArg::Typed(pat) = arg {
168✔
223
            if matches!(*pat.ty, Type::ImplTrait(_)) {
167✔
224
                return true;
1✔
225
            }
226
        }
227
    }
228
    false
452✔
229
}
230

231
fn is_sig_generic(sig: &Signature) -> bool {
461✔
232
    !sig.generics.params.is_empty() || has_generic_arg(sig.inputs.iter())
914✔
233
}
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