Skip to content

Commit c77b4f1

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 153076a commit c77b4f1

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
@@ -126,7 +126,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
126126
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target)
127127
}
128128
[sym::inline] => self.check_inline(hir_id, attr, span, target),
129-
[sym::coverage] => self.check_coverage(hir_id, attr, span, target),
129+
[sym::coverage] => self.check_coverage(attr, span, target),
130130
[sym::non_exhaustive] => self.check_non_exhaustive(hir_id, attr, span, target),
131131
[sym::marker] => self.check_marker(hir_id, attr, span, target),
132132
[sym::target_feature] => {
@@ -388,47 +388,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
388388
}
389389
}
390390

391-
/// Checks if a `#[coverage]` is applied directly to a function
392-
fn check_coverage(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
391+
/// Checks that `#[coverage(..)]` is applied to a function or closure.
392+
fn check_coverage(&self, attr: &Attribute, span: Span, target: Target) -> bool {
393393
match target {
394-
// #[coverage] on function is fine
394+
// #[coverage(..)] on function is fine
395395
Target::Fn
396396
| Target::Closure
397397
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
398-
399-
// function prototypes can't be covered
400-
Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
401-
self.tcx.emit_node_span_lint(
402-
UNUSED_ATTRIBUTES,
403-
hir_id,
404-
attr.span,
405-
errors::IgnoredCoverageFnProto,
406-
);
407-
true
408-
}
409-
410-
Target::Mod | Target::ForeignMod | Target::Impl | Target::Trait => {
411-
self.tcx.emit_node_span_lint(
412-
UNUSED_ATTRIBUTES,
413-
hir_id,
414-
attr.span,
415-
errors::IgnoredCoveragePropagate,
416-
);
417-
true
418-
}
419-
420-
Target::Expression | Target::Statement | Target::Arm => {
421-
self.tcx.emit_node_span_lint(
422-
UNUSED_ATTRIBUTES,
423-
hir_id,
424-
attr.span,
425-
errors::IgnoredCoverageFnDefn,
426-
);
427-
true
428-
}
429-
430398
_ => {
431-
self.dcx().emit_err(errors::IgnoredCoverageNotCoverable {
399+
self.dcx().emit_err(errors::CoverageNotFnOrClosure {
432400
attr_span: attr.span,
433401
defn_span: span,
434402
});

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)