Skip to content

Commit b7c057c

Browse files
committed
coverage: Always error on #[coverage(..)] in unexpected places
This upgrades some warnings to errors, and also catches cases where the attribute was silently ignored.
1 parent a000fa8 commit b7c057c

File tree

3 files changed

+10
-63
lines changed

3 files changed

+10
-63
lines changed

compiler/rustc_passes/messages.ftl

+3-12
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,9 @@ passes_continue_labeled_block =
103103
.label = labeled blocks cannot be `continue`'d
104104
.block_label = labeled block the `continue` points to
105105
106-
passes_coverage_fn_defn =
107-
`#[coverage]` may only be applied to function definitions
108-
109-
passes_coverage_ignored_function_prototype =
110-
`#[coverage]` is ignored on function prototypes
111-
112-
passes_coverage_not_coverable =
113-
`#[coverage]` must be applied to coverable code
114-
.label = not coverable code
115-
116-
passes_coverage_propagate =
117-
`#[coverage]` does not propagate into items and must be applied to the contained functions directly
106+
passes_coverage_not_fn_or_closure =
107+
attribute should be applied to a function definition or closure
108+
.label = not a function or closure
118109
119110
passes_dead_codes =
120111
{ $multiple ->

compiler/rustc_passes/src/check_attr.rs

+5-37
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
122122
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target)
123123
}
124124
[sym::inline] => self.check_inline(hir_id, attr, span, target),
125-
[sym::coverage] => self.check_coverage(hir_id, attr, span, target),
125+
[sym::coverage] => self.check_coverage(attr, span, target),
126126
[sym::non_exhaustive] => self.check_non_exhaustive(hir_id, attr, span, target),
127127
[sym::marker] => self.check_marker(hir_id, attr, span, target),
128128
[sym::target_feature] => {
@@ -369,47 +369,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
369369
}
370370
}
371371

372-
/// Checks if a `#[coverage]` is applied directly to a function
373-
fn check_coverage(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
372+
/// Checks that `#[coverage(..)]` is applied to a function or closure.
373+
fn check_coverage(&self, attr: &Attribute, span: Span, target: Target) -> bool {
374374
match target {
375-
// #[coverage] on function is fine
375+
// #[coverage(..)] on function is fine
376376
Target::Fn
377377
| Target::Closure
378378
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
379-
380-
// function prototypes can't be covered
381-
Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
382-
self.tcx.emit_node_span_lint(
383-
UNUSED_ATTRIBUTES,
384-
hir_id,
385-
attr.span,
386-
errors::IgnoredCoverageFnProto,
387-
);
388-
true
389-
}
390-
391-
Target::Mod | Target::ForeignMod | Target::Impl | Target::Trait => {
392-
self.tcx.emit_node_span_lint(
393-
UNUSED_ATTRIBUTES,
394-
hir_id,
395-
attr.span,
396-
errors::IgnoredCoveragePropagate,
397-
);
398-
true
399-
}
400-
401-
Target::Expression | Target::Statement | Target::Arm => {
402-
self.tcx.emit_node_span_lint(
403-
UNUSED_ATTRIBUTES,
404-
hir_id,
405-
attr.span,
406-
errors::IgnoredCoverageFnDefn,
407-
);
408-
true
409-
}
410-
411379
_ => {
412-
self.dcx().emit_err(errors::IgnoredCoverageNotCoverable {
380+
self.dcx().emit_err(errors::CoverageNotFnOrClosure {
413381
attr_span: attr.span,
414382
defn_span: span,
415383
});

compiler/rustc_passes/src/errors.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,9 @@ pub struct InlineNotFnOrClosure {
6060
pub defn_span: Span,
6161
}
6262

63-
#[derive(LintDiagnostic)]
64-
#[diag(passes_coverage_ignored_function_prototype)]
65-
pub struct IgnoredCoverageFnProto;
66-
67-
#[derive(LintDiagnostic)]
68-
#[diag(passes_coverage_propagate)]
69-
pub struct IgnoredCoveragePropagate;
70-
71-
#[derive(LintDiagnostic)]
72-
#[diag(passes_coverage_fn_defn)]
73-
pub struct IgnoredCoverageFnDefn;
74-
7563
#[derive(Diagnostic)]
76-
#[diag(passes_coverage_not_coverable, code = E0788)]
77-
pub struct IgnoredCoverageNotCoverable {
64+
#[diag(passes_coverage_not_fn_or_closure, code = E0788)]
65+
pub struct CoverageNotFnOrClosure {
7866
#[primary_span]
7967
pub attr_span: Span,
8068
#[label]

0 commit comments

Comments
 (0)