coverage: Always error on #[coverage(..)] in unexpected places

This upgrades some warnings to errors, and also catches cases where the
attribute was silently ignored.
This commit is contained in:
Zalathar 2024-06-19 17:11:27 +10:00
parent a000fa8b54
commit b7c057c9b2
3 changed files with 10 additions and 63 deletions

View File

@ -103,18 +103,9 @@ passes_continue_labeled_block =
.label = labeled blocks cannot be `continue`'d .label = labeled blocks cannot be `continue`'d
.block_label = labeled block the `continue` points to .block_label = labeled block the `continue` points to
passes_coverage_fn_defn = passes_coverage_not_fn_or_closure =
`#[coverage]` may only be applied to function definitions attribute should be applied to a function definition or closure
.label = not a function or closure
passes_coverage_ignored_function_prototype =
`#[coverage]` is ignored on function prototypes
passes_coverage_not_coverable =
`#[coverage]` must be applied to coverable code
.label = not coverable code
passes_coverage_propagate =
`#[coverage]` does not propagate into items and must be applied to the contained functions directly
passes_dead_codes = passes_dead_codes =
{ $multiple -> { $multiple ->

View File

@ -122,7 +122,7 @@ fn check_attributes(
self.check_diagnostic_on_unimplemented(attr.span, hir_id, target) self.check_diagnostic_on_unimplemented(attr.span, hir_id, target)
} }
[sym::inline] => self.check_inline(hir_id, attr, span, target), [sym::inline] => self.check_inline(hir_id, attr, span, target),
[sym::coverage] => self.check_coverage(hir_id, attr, span, target), [sym::coverage] => self.check_coverage(attr, span, target),
[sym::non_exhaustive] => self.check_non_exhaustive(hir_id, attr, span, target), [sym::non_exhaustive] => self.check_non_exhaustive(hir_id, attr, span, target),
[sym::marker] => self.check_marker(hir_id, attr, span, target), [sym::marker] => self.check_marker(hir_id, attr, span, target),
[sym::target_feature] => { [sym::target_feature] => {
@ -369,47 +369,15 @@ fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Targ
} }
} }
/// Checks if a `#[coverage]` is applied directly to a function /// Checks that `#[coverage(..)]` is applied to a function or closure.
fn check_coverage(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool { fn check_coverage(&self, attr: &Attribute, span: Span, target: Target) -> bool {
match target { match target {
// #[coverage] on function is fine // #[coverage(..)] on function is fine
Target::Fn Target::Fn
| Target::Closure | Target::Closure
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true, | Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
// function prototypes can't be covered
Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr.span,
errors::IgnoredCoverageFnProto,
);
true
}
Target::Mod | Target::ForeignMod | Target::Impl | Target::Trait => {
self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr.span,
errors::IgnoredCoveragePropagate,
);
true
}
Target::Expression | Target::Statement | Target::Arm => {
self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr.span,
errors::IgnoredCoverageFnDefn,
);
true
}
_ => { _ => {
self.dcx().emit_err(errors::IgnoredCoverageNotCoverable { self.dcx().emit_err(errors::CoverageNotFnOrClosure {
attr_span: attr.span, attr_span: attr.span,
defn_span: span, defn_span: span,
}); });

View File

@ -60,21 +60,9 @@ pub struct InlineNotFnOrClosure {
pub defn_span: Span, pub defn_span: Span,
} }
#[derive(LintDiagnostic)]
#[diag(passes_coverage_ignored_function_prototype)]
pub struct IgnoredCoverageFnProto;
#[derive(LintDiagnostic)]
#[diag(passes_coverage_propagate)]
pub struct IgnoredCoveragePropagate;
#[derive(LintDiagnostic)]
#[diag(passes_coverage_fn_defn)]
pub struct IgnoredCoverageFnDefn;
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(passes_coverage_not_coverable, code = E0788)] #[diag(passes_coverage_not_fn_or_closure, code = E0788)]
pub struct IgnoredCoverageNotCoverable { pub struct CoverageNotFnOrClosure {
#[primary_span] #[primary_span]
pub attr_span: Span, pub attr_span: Span,
#[label] #[label]