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

xd009642 / tarpaulin / #483

10 May 2024 10:16PM UTC coverage: 73.61% (-0.6%) from 74.182%
#483

push

xd009642
Release 0.30.0

89 of 102 new or added lines in 7 files covered. (87.25%)

7 existing lines in 6 files now uncovered.

2569 of 3490 relevant lines covered (73.61%)

143183.62 hits per line

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

92.7
/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 {
381✔
6
        let mut res = SubResult::Ok;
381✔
7
        for item in items.iter() {
1,073✔
8
            match item {
692✔
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),
446✔
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
381✔
46
    }
47

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

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

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

175
    fn visit_impl(&mut self, impl_blk: &ItemImpl, ctx: &Context) {
14✔
176
        let check_cover = self.check_attr_list(&impl_blk.attrs, ctx);
14✔
177
        if check_cover {
14✔
178
            for item in &impl_blk.items {
41✔
179
                match *item {
180
                    ImplItem::Fn(ref i) => {
13✔
181
                        let item = i.clone();
13✔
182
                        let item_fn = ItemFn {
183
                            attrs: item.attrs,
13✔
184
                            vis: item.vis,
13✔
185
                            sig: item.sig,
13✔
186
                            block: Box::new(item.block),
13✔
187
                        };
188

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

192
                        self.visit_fn(&item_fn, ctx, force_cover);
13✔
193
                    }
194
                    ImplItem::Type(_) => {
1✔
195
                        let analysis = self.get_line_analysis(ctx.file.to_path_buf());
1✔
196
                        analysis.ignore_span(item.span());
1✔
197
                    }
NEW
198
                    _ => {}
×
199
                }
200
            }
201
            self.visit_generics(&impl_blk.generics, ctx);
13✔
202
        } else {
203
            let analysis = self.get_line_analysis(ctx.file.to_path_buf());
1✔
204
            analysis.ignore_tokens(impl_blk);
1✔
205
        }
206
    }
207
}
208

209
fn has_generic_arg<'a>(args: impl Iterator<Item = &'a FnArg>) -> bool {
457✔
210
    for arg in args {
808✔
211
        if let FnArg::Typed(pat) = arg {
172✔
212
            if matches!(*pat.ty, Type::ImplTrait(_)) {
171✔
213
                return true;
1✔
214
            }
215
        }
216
    }
217
    false
456✔
218
}
219

220
fn is_sig_generic(sig: &Signature) -> bool {
465✔
221
    !sig.generics.params.is_empty() || has_generic_arg(sig.inputs.iter())
922✔
222
}
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