1
Fork 0

Emit an error if #[optimize] is applied to an incompatible item

This commit is contained in:
clubby789 2024-07-31 19:39:39 +00:00
parent 99322d84c4
commit 6d7bb127e6
5 changed files with 78 additions and 0 deletions

View file

@ -124,6 +124,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
[sym::inline] => self.check_inline(hir_id, attr, span, target),
[sym::coverage] => self.check_coverage(attr, span, target),
[sym::optimize] => self.check_optimize(hir_id, attr, target),
[sym::non_exhaustive] => self.check_non_exhaustive(hir_id, attr, span, target),
[sym::marker] => self.check_marker(hir_id, attr, span, target),
[sym::target_feature] => {
@ -373,6 +374,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}
}
/// Checks that `#[optimize(..)]` is applied to a function/closure/method,
/// or to an impl block or module.
fn check_optimize(&self, hir_id: HirId, attr: &Attribute, target: Target) {
match target {
Target::Fn
| Target::Closure
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
| Target::Impl
| Target::Mod => {}
_ => {
self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr.span,
errors::OptimizeNotFnOrClosure,
);
}
}
}
fn check_generic_attr(
&self,
hir_id: HirId,

View file

@ -68,6 +68,10 @@ pub struct CoverageNotFnOrClosure {
pub defn_span: Span,
}
#[derive(LintDiagnostic)]
#[diag(passes_optimize_not_fn_or_closure)]
pub struct OptimizeNotFnOrClosure;
#[derive(Diagnostic)]
#[diag(passes_should_be_applied_to_fn)]
pub struct AttrShouldBeAppliedToFn {