2017-06-03 16:14:29 +00:00
|
|
|
//! This module implements some validity checks for attributes.
|
|
|
|
//! In particular it verifies that `#[inline]` and `#[repr]` attributes are
|
|
|
|
//! attached to items that actually support them and if there are
|
|
|
|
//! conflicts between multiple such attributes attached to the same
|
|
|
|
//! item.
|
|
|
|
|
2022-07-11 17:59:04 +00:00
|
|
|
use crate::errors;
|
2022-06-21 11:42:34 +00:00
|
|
|
use rustc_ast::{ast, AttrStyle, Attribute, Lit, LitKind, MetaItemKind, NestedMetaItem};
|
2021-09-05 23:30:37 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2022-07-19 09:00:16 +00:00
|
|
|
use rustc_errors::{fluent, struct_span_err, Applicability, MultiSpan};
|
2022-05-24 18:14:48 +00:00
|
|
|
use rustc_expand::base::resolve_path;
|
2021-09-05 23:30:37 +00:00
|
|
|
use rustc_feature::{AttributeDuplicates, AttributeType, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2022-09-20 05:11:23 +00:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2021-11-03 23:03:12 +00:00
|
|
|
use rustc_hir::intravisit::{self, Visitor};
|
2022-09-20 05:11:23 +00:00
|
|
|
use rustc_hir::{
|
|
|
|
self, FnSig, ForeignItem, HirId, Item, ItemKind, TraitItem, CRATE_HIR_ID, CRATE_OWNER_ID,
|
|
|
|
};
|
2020-01-30 00:24:51 +00:00
|
|
|
use rustc_hir::{MethodKind, Target};
|
2021-11-03 23:03:12 +00:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2022-05-29 18:15:34 +00:00
|
|
|
use rustc_middle::middle::resolve_lifetime::ObjectLifetimeDefault;
|
2021-11-03 23:03:12 +00:00
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2021-03-02 20:10:05 +00:00
|
|
|
use rustc_session::lint::builtin::{
|
2021-03-05 13:44:31 +00:00
|
|
|
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, UNUSED_ATTRIBUTES,
|
2021-03-02 20:10:05 +00:00
|
|
|
};
|
2020-01-22 23:54:04 +00:00
|
|
|
use rustc_session::parse::feature_err;
|
2022-04-05 12:52:53 +00:00
|
|
|
use rustc_span::symbol::{kw, sym, Symbol};
|
2022-03-24 02:03:04 +00:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2022-03-14 10:28:34 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2021-09-05 23:30:37 +00:00
|
|
|
use std::collections::hash_map::Entry;
|
2015-09-25 06:25:59 +00:00
|
|
|
|
2020-05-25 03:07:55 +00:00
|
|
|
pub(crate) fn target_from_impl_item<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
impl_item: &hir::ImplItem<'_>,
|
|
|
|
) -> Target {
|
2020-01-27 18:37:36 +00:00
|
|
|
match impl_item.kind {
|
|
|
|
hir::ImplItemKind::Const(..) => Target::AssocConst,
|
2020-03-05 15:57:34 +00:00
|
|
|
hir::ImplItemKind::Fn(..) => {
|
2022-09-20 05:11:23 +00:00
|
|
|
let parent_def_id = tcx.hir().get_parent_item(impl_item.hir_id()).def_id;
|
2022-09-22 07:11:51 +00:00
|
|
|
let containing_item = tcx.hir().expect_item(parent_def_id);
|
2020-01-27 18:37:36 +00:00
|
|
|
let containing_impl_is_for_trait = match &containing_item.kind {
|
2020-11-22 22:46:21 +00:00
|
|
|
hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(),
|
2020-01-27 18:37:36 +00:00
|
|
|
_ => bug!("parent of an ImplItem must be an Impl"),
|
|
|
|
};
|
|
|
|
if containing_impl_is_for_trait {
|
|
|
|
Target::Method(MethodKind::Trait { body: true })
|
|
|
|
} else {
|
|
|
|
Target::Method(MethodKind::Inherent)
|
2019-10-24 21:21:30 +00:00
|
|
|
}
|
2019-10-13 15:14:59 +00:00
|
|
|
}
|
2020-05-10 11:15:51 +00:00
|
|
|
hir::ImplItemKind::TyAlias(..) => Target::AssocTy,
|
2019-10-13 15:14:59 +00:00
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 16:44:32 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum ItemLike<'tcx> {
|
|
|
|
Item(&'tcx Item<'tcx>),
|
2022-07-25 20:36:03 +00:00
|
|
|
ForeignItem,
|
2020-09-29 16:44:32 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 19:03:44 +00:00
|
|
|
struct CheckAttrVisitor<'tcx> {
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 05:16:36 +00:00
|
|
|
impl CheckAttrVisitor<'_> {
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Checks any attribute.
|
2019-10-11 01:33:16 +00:00
|
|
|
fn check_attributes(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
2022-01-05 12:02:16 +00:00
|
|
|
span: Span,
|
2019-10-11 01:33:16 +00:00
|
|
|
target: Target,
|
2020-09-29 16:44:32 +00:00
|
|
|
item: Option<ItemLike<'_>>,
|
2019-10-11 01:33:16 +00:00
|
|
|
) {
|
2021-10-01 18:54:36 +00:00
|
|
|
let mut doc_aliases = FxHashMap::default();
|
2019-09-26 10:00:53 +00:00
|
|
|
let mut is_valid = true;
|
2021-05-08 14:57:10 +00:00
|
|
|
let mut specified_inline = None;
|
2021-09-05 23:30:37 +00:00
|
|
|
let mut seen = FxHashMap::default();
|
2020-11-25 22:27:23 +00:00
|
|
|
let attrs = self.tcx.hir().attrs(hir_id);
|
2019-10-11 01:33:16 +00:00
|
|
|
for attr in attrs {
|
2021-07-29 17:00:41 +00:00
|
|
|
let attr_is_valid = match attr.name_or_empty() {
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::inline => self.check_inline(hir_id, attr, span, target),
|
2022-05-28 21:20:43 +00:00
|
|
|
sym::no_coverage => self.check_no_coverage(hir_id, attr, span, target),
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::non_exhaustive => self.check_non_exhaustive(hir_id, attr, span, target),
|
|
|
|
sym::marker => self.check_marker(hir_id, attr, span, target),
|
2022-01-27 18:25:51 +00:00
|
|
|
sym::rustc_must_implement_one_of => {
|
|
|
|
self.check_rustc_must_implement_one_of(attr, span, target)
|
|
|
|
}
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::target_feature => self.check_target_feature(hir_id, attr, span, target),
|
2022-03-16 00:00:00 +00:00
|
|
|
sym::thread_local => self.check_thread_local(attr, span, target),
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::track_caller => {
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_track_caller(hir_id, attr.span, attrs, span, target)
|
2020-07-11 14:29:35 +00:00
|
|
|
}
|
2021-10-01 18:54:36 +00:00
|
|
|
sym::doc => self.check_doc_attrs(
|
|
|
|
attr,
|
|
|
|
hir_id,
|
|
|
|
target,
|
|
|
|
&mut specified_inline,
|
|
|
|
&mut doc_aliases,
|
|
|
|
),
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::no_link => self.check_no_link(hir_id, &attr, span, target),
|
|
|
|
sym::export_name => self.check_export_name(hir_id, &attr, span, target),
|
|
|
|
sym::rustc_layout_scalar_valid_range_start
|
|
|
|
| sym::rustc_layout_scalar_valid_range_end => {
|
|
|
|
self.check_rustc_layout_scalar_valid_range(&attr, span, target)
|
|
|
|
}
|
|
|
|
sym::allow_internal_unstable => {
|
|
|
|
self.check_allow_internal_unstable(hir_id, &attr, span, target, &attrs)
|
|
|
|
}
|
2022-04-26 01:02:43 +00:00
|
|
|
sym::debugger_visualizer => self.check_debugger_visualizer(&attr, target),
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::rustc_allow_const_fn_unstable => {
|
|
|
|
self.check_rustc_allow_const_fn_unstable(hir_id, &attr, span, target)
|
|
|
|
}
|
2022-05-04 09:18:37 +00:00
|
|
|
sym::rustc_std_internal_symbol => {
|
|
|
|
self.check_rustc_std_internal_symbol(&attr, span, target)
|
|
|
|
}
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::naked => self.check_naked(hir_id, attr, span, target),
|
|
|
|
sym::rustc_legacy_const_generics => {
|
|
|
|
self.check_rustc_legacy_const_generics(&attr, span, target, item)
|
|
|
|
}
|
2022-01-05 12:02:16 +00:00
|
|
|
sym::rustc_lint_query_instability => {
|
|
|
|
self.check_rustc_lint_query_instability(&attr, span, target)
|
|
|
|
}
|
2022-06-10 14:50:06 +00:00
|
|
|
sym::rustc_lint_diagnostics => {
|
|
|
|
self.check_rustc_lint_diagnostics(&attr, span, target)
|
|
|
|
}
|
2022-07-25 12:02:39 +00:00
|
|
|
sym::rustc_lint_opt_ty => self.check_rustc_lint_opt_ty(&attr, span, target),
|
|
|
|
sym::rustc_lint_opt_deny_field_access => {
|
|
|
|
self.check_rustc_lint_opt_deny_field_access(&attr, span, target)
|
|
|
|
}
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::rustc_clean
|
|
|
|
| sym::rustc_dirty
|
|
|
|
| sym::rustc_if_this_changed
|
|
|
|
| sym::rustc_then_this_would_need => self.check_rustc_dirty_clean(&attr),
|
2021-06-25 13:36:38 +00:00
|
|
|
sym::cmse_nonsecure_entry => self.check_cmse_nonsecure_entry(attr, span, target),
|
2022-07-21 15:19:22 +00:00
|
|
|
sym::collapse_debuginfo => self.check_collapse_debuginfo(attr, span, target),
|
2022-03-16 09:49:54 +00:00
|
|
|
sym::const_trait => self.check_const_trait(attr, span, target),
|
2021-09-05 02:36:51 +00:00
|
|
|
sym::must_not_suspend => self.check_must_not_suspend(&attr, span, target),
|
2021-10-06 20:19:39 +00:00
|
|
|
sym::must_use => self.check_must_use(hir_id, &attr, span, target),
|
2022-01-07 11:38:16 +00:00
|
|
|
sym::rustc_pass_by_value => self.check_pass_by_value(&attr, span, target),
|
2022-03-15 15:30:30 +00:00
|
|
|
sym::rustc_allow_incoherent_impl => {
|
|
|
|
self.check_allow_incoherent_impl(&attr, span, target)
|
|
|
|
}
|
2022-04-28 14:26:30 +00:00
|
|
|
sym::rustc_has_incoherent_inherent_impls => {
|
|
|
|
self.check_has_incoherent_inherent_impls(&attr, span, target)
|
|
|
|
}
|
2021-07-29 17:00:41 +00:00
|
|
|
sym::rustc_const_unstable
|
|
|
|
| sym::rustc_const_stable
|
|
|
|
| sym::unstable
|
|
|
|
| sym::stable
|
2022-05-09 22:18:53 +00:00
|
|
|
| sym::rustc_allowed_through_unstable_modules
|
2021-07-29 17:00:41 +00:00
|
|
|
| sym::rustc_promotable => self.check_stability_promotable(&attr, span, target),
|
2022-08-03 04:19:21 +00:00
|
|
|
sym::link_ordinal => self.check_link_ordinal(&attr, span, target),
|
2021-04-11 00:00:00 +00:00
|
|
|
_ => true,
|
2019-09-26 10:00:53 +00:00
|
|
|
};
|
2021-07-29 17:00:41 +00:00
|
|
|
is_valid &= attr_is_valid;
|
|
|
|
|
2021-04-11 00:00:00 +00:00
|
|
|
// lint-only checks
|
|
|
|
match attr.name_or_empty() {
|
|
|
|
sym::cold => self.check_cold(hir_id, attr, span, target),
|
2022-01-09 19:28:40 +00:00
|
|
|
sym::link => self.check_link(hir_id, attr, span, target),
|
2021-04-11 00:00:00 +00:00
|
|
|
sym::link_name => self.check_link_name(hir_id, attr, span, target),
|
|
|
|
sym::link_section => self.check_link_section(hir_id, attr, span, target),
|
|
|
|
sym::no_mangle => self.check_no_mangle(hir_id, attr, span, target),
|
2022-06-14 23:11:24 +00:00
|
|
|
sym::deprecated => self.check_deprecated(hir_id, attr, span, target),
|
2021-07-29 17:00:41 +00:00
|
|
|
sym::macro_use | sym::macro_escape => self.check_macro_use(hir_id, attr, target),
|
|
|
|
sym::path => self.check_generic_attr(hir_id, attr, target, &[Target::Mod]),
|
|
|
|
sym::plugin_registrar => self.check_plugin_registrar(hir_id, attr, target),
|
|
|
|
sym::macro_export => self.check_macro_export(hir_id, attr, target),
|
|
|
|
sym::ignore | sym::should_panic | sym::proc_macro_derive => {
|
|
|
|
self.check_generic_attr(hir_id, attr, target, &[Target::Fn])
|
|
|
|
}
|
2021-08-21 19:11:36 +00:00
|
|
|
sym::automatically_derived => {
|
|
|
|
self.check_generic_attr(hir_id, attr, target, &[Target::Impl])
|
|
|
|
}
|
|
|
|
sym::no_implicit_prelude => {
|
|
|
|
self.check_generic_attr(hir_id, attr, target, &[Target::Mod])
|
|
|
|
}
|
2022-08-30 20:47:58 +00:00
|
|
|
sym::rustc_object_lifetime_default => self.check_object_lifetime_default(hir_id),
|
2021-07-29 17:00:41 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
|
2021-09-05 23:30:37 +00:00
|
|
|
let builtin = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
|
|
|
|
|
2021-07-29 17:00:41 +00:00
|
|
|
if hir_id != CRATE_HIR_ID {
|
2021-11-12 12:15:14 +00:00
|
|
|
if let Some(BuiltinAttribute { type_: AttributeType::CrateLevel, .. }) =
|
2021-07-29 17:00:41 +00:00
|
|
|
attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name))
|
|
|
|
{
|
2022-07-11 17:59:04 +00:00
|
|
|
match attr.style {
|
|
|
|
ast::AttrStyle::Outer => self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::OuterCrateLevelAttr,
|
|
|
|
),
|
|
|
|
ast::AttrStyle::Inner => self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::InnerCrateLevelAttr,
|
|
|
|
),
|
|
|
|
}
|
2021-07-29 17:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 23:30:37 +00:00
|
|
|
if let Some(BuiltinAttribute { duplicates, .. }) = builtin {
|
|
|
|
check_duplicates(self.tcx, attr, hir_id, *duplicates, &mut seen);
|
|
|
|
}
|
|
|
|
|
2022-03-03 21:28:56 +00:00
|
|
|
self.check_unused_attribute(hir_id, attr)
|
2019-09-26 10:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !is_valid {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-04 09:18:37 +00:00
|
|
|
// FIXME(@lcnr): this doesn't belong here.
|
2022-08-02 18:36:41 +00:00
|
|
|
if matches!(
|
|
|
|
target,
|
|
|
|
Target::Closure
|
|
|
|
| Target::Fn
|
|
|
|
| Target::Method(_)
|
|
|
|
| Target::ForeignFn
|
|
|
|
| Target::ForeignStatic
|
|
|
|
) {
|
2020-05-01 09:32:20 +00:00
|
|
|
self.tcx.ensure().codegen_fn_attrs(self.tcx.hir().local_def_id(hir_id));
|
2017-06-03 16:14:29 +00:00
|
|
|
}
|
2018-01-08 21:43:42 +00:00
|
|
|
|
2020-01-27 23:27:57 +00:00
|
|
|
self.check_repr(attrs, span, target, item, hir_id);
|
2019-10-11 01:33:16 +00:00
|
|
|
self.check_used(attrs, target);
|
2017-06-03 16:14:29 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 14:35:53 +00:00
|
|
|
fn inline_attr_str_error_with_macro_def(&self, hir_id: HirId, attr: &Attribute, sym: &str) {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::IgnoredAttrWithMacro { sym },
|
|
|
|
);
|
2021-02-01 14:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inline_attr_str_error_without_macro_def(&self, hir_id: HirId, attr: &Attribute, sym: &str) {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::IgnoredAttr { sym },
|
|
|
|
);
|
2021-02-01 14:35:53 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 10:00:53 +00:00
|
|
|
/// Checks if an `#[inline]` is applied to a function or a closure. Returns `true` if valid.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_inline(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
|
2019-10-11 01:33:16 +00:00
|
|
|
match target {
|
2019-10-24 21:21:30 +00:00
|
|
|
Target::Fn
|
|
|
|
| Target::Closure
|
2020-04-17 00:38:52 +00:00
|
|
|
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
|
2019-10-24 21:21:30 +00:00
|
|
|
Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::IgnoredInlineAttrFnProto,
|
|
|
|
);
|
2019-10-11 01:35:22 +00:00
|
|
|
true
|
|
|
|
}
|
2019-10-25 23:46:07 +00:00
|
|
|
// FIXME(#65833): We permit associated consts to have an `#[inline]` attribute with
|
|
|
|
// just a lint, because we previously erroneously allowed it and some crates used it
|
|
|
|
// accidentally, to to be compatible with crates depending on them, we can't throw an
|
|
|
|
// error here.
|
|
|
|
Target::AssocConst => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::IgnoredInlineAttrConstants,
|
|
|
|
);
|
2019-10-25 23:46:07 +00:00
|
|
|
true
|
|
|
|
}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): Same for fields, arms, and macro defs
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "inline");
|
|
|
|
true
|
|
|
|
}
|
2019-10-11 01:33:16 +00:00
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::InlineNotFnOrClosure {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2019-10-11 01:33:16 +00:00
|
|
|
false
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 21:20:43 +00:00
|
|
|
/// Checks if a `#[no_coverage]` is applied directly to a function
|
|
|
|
fn check_no_coverage(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
|
|
|
match target {
|
|
|
|
// no_coverage on function is fine
|
|
|
|
Target::Fn
|
|
|
|
| Target::Closure
|
|
|
|
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
|
|
|
|
|
|
|
|
// function prototypes can't be covered
|
|
|
|
Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::IgnoredNoCoverageFnProto,
|
|
|
|
);
|
2022-05-28 21:20:43 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
Target::Mod | Target::ForeignMod | Target::Impl | Target::Trait => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::IgnoredNoCoveragePropagate,
|
|
|
|
);
|
2022-05-28 21:20:43 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
Target::Expression | Target::Statement | Target::Arm => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::IgnoredNoCoverageFnDefn,
|
|
|
|
);
|
2022-05-28 21:20:43 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::IgnoredNoCoverageNotCoverable {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2022-05-28 21:20:43 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 17:00:41 +00:00
|
|
|
fn check_generic_attr(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr: &Attribute,
|
|
|
|
target: Target,
|
|
|
|
allowed_targets: &[Target],
|
|
|
|
) {
|
|
|
|
if !allowed_targets.iter().any(|t| t == &target) {
|
|
|
|
let name = attr.name_or_empty();
|
|
|
|
let mut i = allowed_targets.iter();
|
|
|
|
// Pluralize
|
|
|
|
let b = i.next().map_or_else(String::new, |t| t.to_string() + "s");
|
|
|
|
let supported_names = i.enumerate().fold(b, |mut b, (i, allowed_target)| {
|
|
|
|
if allowed_targets.len() > 2 && i == allowed_targets.len() - 2 {
|
|
|
|
b.push_str(", and ");
|
|
|
|
} else if allowed_targets.len() == 2 && i == allowed_targets.len() - 2 {
|
|
|
|
b.push_str(" and ");
|
|
|
|
} else {
|
|
|
|
b.push_str(", ");
|
|
|
|
}
|
|
|
|
// Pluralize
|
|
|
|
b.push_str(&(allowed_target.to_string() + "s"));
|
|
|
|
b
|
|
|
|
});
|
|
|
|
self.tcx.struct_span_lint_hir(UNUSED_ATTRIBUTES, hir_id, attr.span, |lint| {
|
|
|
|
lint.build(&format!("`#[{name}]` only has an effect on {}", supported_names))
|
|
|
|
.emit();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 00:00:00 +00:00
|
|
|
/// Checks if `#[naked]` is applied to a function definition.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_naked(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
|
2020-11-24 00:00:00 +00:00
|
|
|
match target {
|
|
|
|
Target::Fn
|
|
|
|
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[allow_internal_unstable]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "naked");
|
|
|
|
true
|
|
|
|
}
|
2020-11-24 00:00:00 +00:00
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToFn {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2020-11-24 00:00:00 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 13:36:38 +00:00
|
|
|
/// Checks if `#[cmse_nonsecure_entry]` is applied to a function definition.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_cmse_nonsecure_entry(&self, attr: &Attribute, span: Span, target: Target) -> bool {
|
2021-06-25 13:36:38 +00:00
|
|
|
match target {
|
|
|
|
Target::Fn
|
|
|
|
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
|
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToFn {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2021-06-25 13:36:38 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-29 18:15:34 +00:00
|
|
|
/// Debugging aid for `object_lifetime_default` query.
|
2022-08-30 20:47:58 +00:00
|
|
|
fn check_object_lifetime_default(&self, hir_id: HirId) {
|
2022-05-29 18:15:34 +00:00
|
|
|
let tcx = self.tcx;
|
|
|
|
if let Some(generics) = tcx.hir().get_generics(tcx.hir().local_def_id(hir_id)) {
|
2022-08-30 20:47:58 +00:00
|
|
|
for p in generics.params {
|
|
|
|
let hir::GenericParamKind::Type { .. } = p.kind else { continue };
|
|
|
|
let param_id = tcx.hir().local_def_id(p.hir_id);
|
|
|
|
let default = tcx.object_lifetime_default(param_id);
|
|
|
|
let repr = match default {
|
|
|
|
ObjectLifetimeDefault::Empty => "BaseDefault".to_owned(),
|
|
|
|
ObjectLifetimeDefault::Static => "'static".to_owned(),
|
|
|
|
ObjectLifetimeDefault::Param(def_id) => tcx.item_name(def_id).to_string(),
|
|
|
|
ObjectLifetimeDefault::Ambiguous => "Ambiguous".to_owned(),
|
|
|
|
};
|
|
|
|
tcx.sess.span_err(p.span, &repr);
|
|
|
|
}
|
2022-05-29 18:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:19:22 +00:00
|
|
|
/// Checks if `#[collapse_debuginfo]` is applied to a macro.
|
|
|
|
fn check_collapse_debuginfo(&self, attr: &Attribute, span: Span, target: Target) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::MacroDef => true,
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.emit_err(errors::CollapseDebuginfo { attr_span: attr.span, defn_span: span });
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 01:24:10 +00:00
|
|
|
/// Checks if a `#[track_caller]` is applied to a non-naked function. Returns `true` if valid.
|
2019-10-11 01:42:23 +00:00
|
|
|
fn check_track_caller(
|
|
|
|
&self,
|
2021-02-01 14:35:53 +00:00
|
|
|
hir_id: HirId,
|
2022-01-05 12:02:16 +00:00
|
|
|
attr_span: Span,
|
2021-12-14 05:16:36 +00:00
|
|
|
attrs: &[Attribute],
|
2022-01-05 12:02:16 +00:00
|
|
|
span: Span,
|
2019-10-11 01:42:23 +00:00
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2019-10-14 14:07:16 +00:00
|
|
|
match target {
|
2020-11-24 00:00:00 +00:00
|
|
|
_ if attrs.iter().any(|attr| attr.has_name(sym::naked)) => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::NakedTrackedCaller { attr_span });
|
2019-10-14 14:07:16 +00:00
|
|
|
false
|
|
|
|
}
|
2020-08-12 16:43:14 +00:00
|
|
|
Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => true,
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[track_caller]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
for attr in attrs {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "track_caller");
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2019-10-14 14:07:16 +00:00
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.emit_err(errors::TrackedCallerWrongLocation { attr_span, defn_span: span });
|
2019-10-14 14:07:16 +00:00
|
|
|
false
|
|
|
|
}
|
2019-07-20 00:55:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:00:53 +00:00
|
|
|
/// Checks if the `#[non_exhaustive]` attribute on an `item` is valid. Returns `true` if valid.
|
2021-02-01 14:35:53 +00:00
|
|
|
fn check_non_exhaustive(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr: &Attribute,
|
2022-01-05 12:02:16 +00:00
|
|
|
span: Span,
|
2021-02-01 14:35:53 +00:00
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2018-03-24 18:07:18 +00:00
|
|
|
match target {
|
2020-09-10 18:54:17 +00:00
|
|
|
Target::Struct | Target::Enum | Target::Variant => true,
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[non_exhaustive]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "non_exhaustive");
|
|
|
|
true
|
|
|
|
}
|
2018-03-24 18:07:18 +00:00
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::NonExhaustiveWrongLocation {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2019-09-26 10:00:53 +00:00
|
|
|
false
|
2018-03-24 18:07:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:00:53 +00:00
|
|
|
/// Checks if the `#[marker]` attribute on an `item` is valid. Returns `true` if valid.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_marker(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
|
2018-08-25 07:54:41 +00:00
|
|
|
match target {
|
2019-09-26 10:00:53 +00:00
|
|
|
Target::Trait => true,
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[marker]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "marker");
|
|
|
|
true
|
|
|
|
}
|
2018-08-25 07:54:41 +00:00
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToTrait {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2019-09-26 10:00:53 +00:00
|
|
|
false
|
2018-08-25 07:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-27 18:25:51 +00:00
|
|
|
/// Checks if the `#[rustc_must_implement_one_of]` attribute on a `target` is valid. Returns `true` if valid.
|
|
|
|
fn check_rustc_must_implement_one_of(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
2022-02-01 09:29:36 +00:00
|
|
|
span: Span,
|
2022-01-27 18:25:51 +00:00
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::Trait => true,
|
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToTrait {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2022-01-27 18:25:51 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 10:00:53 +00:00
|
|
|
/// Checks if the `#[target_feature]` attribute on `item` is valid. Returns `true` if valid.
|
2020-07-11 14:29:35 +00:00
|
|
|
fn check_target_feature(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr: &Attribute,
|
2022-01-05 12:02:16 +00:00
|
|
|
span: Span,
|
2020-07-11 14:29:35 +00:00
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2019-09-26 10:00:53 +00:00
|
|
|
match target {
|
2019-10-24 21:21:30 +00:00
|
|
|
Target::Fn
|
2020-04-17 00:38:52 +00:00
|
|
|
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => true,
|
2020-07-11 14:29:35 +00:00
|
|
|
// FIXME: #[target_feature] was previously erroneously allowed on statements and some
|
|
|
|
// crates used this, so only emit a warning.
|
|
|
|
Target::Statement => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::TargetFeatureOnStatement,
|
|
|
|
);
|
2020-07-11 14:29:35 +00:00
|
|
|
true
|
|
|
|
}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[target_feature]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "target_feature");
|
|
|
|
true
|
|
|
|
}
|
2019-09-26 10:00:53 +00:00
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToFn {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2019-09-26 10:00:53 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:00:00 +00:00
|
|
|
/// Checks if the `#[thread_local]` attribute on `item` is valid. Returns `true` if valid.
|
|
|
|
fn check_thread_local(&self, attr: &Attribute, span: Span, target: Target) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::ForeignStatic | Target::Static => true,
|
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToStatic {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2022-03-16 00:00:00 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 13:19:53 +00:00
|
|
|
fn doc_attr_str_error(&self, meta: &NestedMetaItem, attr_name: &str) {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DocExpectStr { attr_span: meta.span(), attr_name });
|
2020-09-16 14:41:45 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 20:59:01 +00:00
|
|
|
fn check_doc_alias_value(
|
|
|
|
&self,
|
|
|
|
meta: &NestedMetaItem,
|
2022-04-05 12:52:53 +00:00
|
|
|
doc_alias: Symbol,
|
2021-03-06 20:59:01 +00:00
|
|
|
hir_id: HirId,
|
|
|
|
target: Target,
|
|
|
|
is_list: bool,
|
2021-10-01 18:54:36 +00:00
|
|
|
aliases: &mut FxHashMap<String, Span>,
|
2021-03-06 20:59:01 +00:00
|
|
|
) -> bool {
|
2021-03-08 16:30:22 +00:00
|
|
|
let tcx = self.tcx;
|
2022-07-11 17:59:04 +00:00
|
|
|
let span = meta.name_value_literal_span().unwrap_or_else(|| meta.span());
|
|
|
|
let attr_str =
|
|
|
|
&format!("`#[doc(alias{})]`", if is_list { "(\"...\")" } else { " = \"...\"" });
|
2022-04-05 12:52:53 +00:00
|
|
|
if doc_alias == kw::Empty {
|
2022-07-11 17:59:04 +00:00
|
|
|
tcx.sess.emit_err(errors::DocAliasEmpty { span, attr_str });
|
|
|
|
return false;
|
2020-12-01 13:11:33 +00:00
|
|
|
}
|
2022-04-05 12:52:53 +00:00
|
|
|
|
|
|
|
let doc_alias_str = doc_alias.as_str();
|
|
|
|
if let Some(c) = doc_alias_str
|
|
|
|
.chars()
|
|
|
|
.find(|&c| c == '"' || c == '\'' || (c.is_whitespace() && c != ' '))
|
2020-12-01 13:11:33 +00:00
|
|
|
{
|
2022-07-11 17:59:04 +00:00
|
|
|
tcx.sess.emit_err(errors::DocAliasBadChar { span, attr_str, char_: c });
|
2020-12-01 13:11:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-04-05 12:52:53 +00:00
|
|
|
if doc_alias_str.starts_with(' ') || doc_alias_str.ends_with(' ') {
|
2022-07-11 17:59:04 +00:00
|
|
|
tcx.sess.emit_err(errors::DocAliasStartEnd { span, attr_str });
|
|
|
|
return false;
|
2020-12-01 13:11:33 +00:00
|
|
|
}
|
2022-07-11 17:59:04 +00:00
|
|
|
|
|
|
|
let span = meta.span();
|
|
|
|
if let Some(location) = match target {
|
2020-12-01 13:11:33 +00:00
|
|
|
Target::AssocTy => {
|
2022-09-20 05:11:23 +00:00
|
|
|
let parent_def_id = self.tcx.hir().get_parent_item(hir_id).def_id;
|
2022-09-22 07:11:51 +00:00
|
|
|
let containing_item = self.tcx.hir().expect_item(parent_def_id);
|
2020-12-01 13:11:33 +00:00
|
|
|
if Target::from_item(containing_item) == Target::Impl {
|
|
|
|
Some("type alias in implementation block")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Target::AssocConst => {
|
2022-09-20 05:11:23 +00:00
|
|
|
let parent_def_id = self.tcx.hir().get_parent_item(hir_id).def_id;
|
2022-09-22 07:11:51 +00:00
|
|
|
let containing_item = self.tcx.hir().expect_item(parent_def_id);
|
2020-12-01 13:11:33 +00:00
|
|
|
// We can't link to trait impl's consts.
|
|
|
|
let err = "associated constant in trait implementation block";
|
|
|
|
match containing_item.kind {
|
2020-11-22 22:46:21 +00:00
|
|
|
ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => Some(err),
|
2020-12-01 13:11:33 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2021-06-15 17:01:11 +00:00
|
|
|
// we check the validity of params elsewhere
|
|
|
|
Target::Param => return false,
|
2022-08-03 03:11:22 +00:00
|
|
|
Target::Expression
|
|
|
|
| Target::Statement
|
|
|
|
| Target::Arm
|
|
|
|
| Target::ForeignMod
|
|
|
|
| Target::Closure
|
|
|
|
| Target::Impl => Some(target.name()),
|
2022-08-02 14:05:11 +00:00
|
|
|
Target::ExternCrate
|
|
|
|
| Target::Use
|
|
|
|
| Target::Static
|
|
|
|
| Target::Const
|
|
|
|
| Target::Fn
|
|
|
|
| Target::Mod
|
|
|
|
| Target::GlobalAsm
|
|
|
|
| Target::TyAlias
|
|
|
|
| Target::OpaqueTy
|
2022-08-31 03:04:44 +00:00
|
|
|
| Target::ImplTraitPlaceholder
|
2022-08-02 14:05:11 +00:00
|
|
|
| Target::Enum
|
|
|
|
| Target::Variant
|
|
|
|
| Target::Struct
|
|
|
|
| Target::Field
|
|
|
|
| Target::Union
|
|
|
|
| Target::Trait
|
|
|
|
| Target::TraitAlias
|
|
|
|
| Target::Method(..)
|
|
|
|
| Target::ForeignFn
|
|
|
|
| Target::ForeignStatic
|
|
|
|
| Target::ForeignTy
|
|
|
|
| Target::GenericParam(..)
|
2022-05-03 18:52:53 +00:00
|
|
|
| Target::MacroDef
|
2022-05-03 20:23:03 +00:00
|
|
|
| Target::PatField
|
|
|
|
| Target::ExprField => None,
|
2020-12-01 13:11:33 +00:00
|
|
|
} {
|
2022-07-11 17:59:04 +00:00
|
|
|
tcx.sess.emit_err(errors::DocAliasBadLocation { span, attr_str, location });
|
|
|
|
return false;
|
2020-12-01 13:11:33 +00:00
|
|
|
}
|
2021-01-04 14:05:36 +00:00
|
|
|
let item_name = self.tcx.hir().name(hir_id);
|
2022-04-05 12:52:53 +00:00
|
|
|
if item_name == doc_alias {
|
2022-07-11 17:59:04 +00:00
|
|
|
tcx.sess.emit_err(errors::DocAliasNotAnAlias { span, attr_str });
|
|
|
|
return false;
|
2021-01-04 14:05:36 +00:00
|
|
|
}
|
2022-04-05 12:52:53 +00:00
|
|
|
if let Err(entry) = aliases.try_insert(doc_alias_str.to_owned(), span) {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
span,
|
|
|
|
errors::DocAliasDuplicated { first_defn: *entry.entry.get() },
|
|
|
|
);
|
2021-10-01 18:54:36 +00:00
|
|
|
}
|
2020-12-01 13:11:33 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-10-01 18:54:36 +00:00
|
|
|
fn check_doc_alias(
|
|
|
|
&self,
|
|
|
|
meta: &NestedMetaItem,
|
|
|
|
hir_id: HirId,
|
|
|
|
target: Target,
|
|
|
|
aliases: &mut FxHashMap<String, Span>,
|
|
|
|
) -> bool {
|
2021-03-06 20:59:01 +00:00
|
|
|
if let Some(values) = meta.meta_item_list() {
|
|
|
|
let mut errors = 0;
|
|
|
|
for v in values {
|
|
|
|
match v.literal() {
|
|
|
|
Some(l) => match l.kind {
|
|
|
|
LitKind::Str(s, _) => {
|
2022-04-05 12:52:53 +00:00
|
|
|
if !self.check_doc_alias_value(v, s, hir_id, target, true, aliases) {
|
2021-03-06 20:59:01 +00:00
|
|
|
errors += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
2022-07-11 17:59:04 +00:00
|
|
|
.emit_err(errors::DocAliasNotStringLiteral { span: v.span() });
|
2021-03-06 20:59:01 +00:00
|
|
|
errors += 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DocAliasNotStringLiteral { span: v.span() });
|
2021-03-06 20:59:01 +00:00
|
|
|
errors += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errors == 0
|
2022-04-05 12:52:53 +00:00
|
|
|
} else if let Some(doc_alias) = meta.value_str() {
|
|
|
|
self.check_doc_alias_value(meta, doc_alias, hir_id, target, false, aliases)
|
2021-03-06 20:59:01 +00:00
|
|
|
} else {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DocAliasMalformed { span: meta.span() });
|
2021-03-06 20:59:01 +00:00
|
|
|
false
|
2021-01-04 14:05:36 +00:00
|
|
|
}
|
2020-12-01 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2020-12-01 13:19:53 +00:00
|
|
|
fn check_doc_keyword(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
|
2022-04-05 12:52:53 +00:00
|
|
|
let doc_keyword = meta.value_str().unwrap_or(kw::Empty);
|
|
|
|
if doc_keyword == kw::Empty {
|
2020-12-01 13:19:53 +00:00
|
|
|
self.doc_attr_str_error(meta, "keyword");
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-17 14:45:26 +00:00
|
|
|
match self.tcx.hir().find(hir_id).and_then(|node| match node {
|
|
|
|
hir::Node::Item(item) => Some(&item.kind),
|
|
|
|
_ => None,
|
|
|
|
}) {
|
|
|
|
Some(ItemKind::Mod(ref module)) => {
|
2020-12-01 13:19:53 +00:00
|
|
|
if !module.item_ids.is_empty() {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DocKeywordEmptyMod { span: meta.span() });
|
2020-12-01 13:19:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DocKeywordNotMod { span: meta.span() });
|
2020-12-01 13:19:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 12:52:53 +00:00
|
|
|
if !rustc_lexer::is_ident(doc_keyword.as_str()) {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DocKeywordInvalidIdent {
|
|
|
|
span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
|
|
|
|
doc_keyword,
|
|
|
|
});
|
2020-12-01 13:19:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2022-06-16 16:50:57 +00:00
|
|
|
fn check_doc_fake_variadic(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
|
2022-06-09 02:26:51 +00:00
|
|
|
match self.tcx.hir().find(hir_id).and_then(|node| match node {
|
|
|
|
hir::Node::Item(item) => Some(&item.kind),
|
|
|
|
_ => None,
|
|
|
|
}) {
|
|
|
|
Some(ItemKind::Impl(ref i)) => {
|
2022-06-16 16:50:57 +00:00
|
|
|
let is_valid = matches!(&i.self_ty.kind, hir::TyKind::Tup([_]))
|
|
|
|
|| if let hir::TyKind::BareFn(bare_fn_ty) = &i.self_ty.kind {
|
|
|
|
bare_fn_ty.decl.inputs.len() == 1
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if !is_valid {
|
|
|
|
self.tcx.sess.emit_err(errors::DocFakeVariadicNotValid { span: meta.span() });
|
2022-06-09 02:26:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DocKeywordOnlyImpl { span: meta.span() });
|
2022-06-09 02:26:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:38:09 +00:00
|
|
|
/// Checks `#[doc(inline)]`/`#[doc(no_inline)]` attributes. Returns `true` if valid.
|
|
|
|
///
|
|
|
|
/// A doc inlining attribute is invalid if it is applied to a non-`use` item, or
|
|
|
|
/// if there are conflicting attributes for one item.
|
|
|
|
///
|
|
|
|
/// `specified_inline` is used to keep track of whether we have
|
|
|
|
/// already seen an inlining attribute for this item.
|
|
|
|
/// If so, `specified_inline` holds the value and the span of
|
|
|
|
/// the first `inline`/`no_inline` attribute.
|
2021-05-08 14:57:10 +00:00
|
|
|
fn check_doc_inline(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
meta: &NestedMetaItem,
|
|
|
|
hir_id: HirId,
|
|
|
|
target: Target,
|
|
|
|
specified_inline: &mut Option<(bool, Span)>,
|
|
|
|
) -> bool {
|
2021-06-06 01:16:19 +00:00
|
|
|
if target == Target::Use || target == Target::ExternCrate {
|
2021-05-08 14:57:10 +00:00
|
|
|
let do_inline = meta.name_or_empty() == sym::inline;
|
|
|
|
if let Some((prev_inline, prev_span)) = *specified_inline {
|
|
|
|
if do_inline != prev_inline {
|
|
|
|
let mut spans = MultiSpan::from_spans(vec![prev_span, meta.span()]);
|
2022-07-11 17:59:04 +00:00
|
|
|
spans.push_span_label(prev_span, fluent::passes::doc_inline_conflict_first);
|
|
|
|
spans.push_span_label(meta.span(), fluent::passes::doc_inline_conflict_second);
|
|
|
|
self.tcx.sess.emit_err(errors::DocKeywordConflict { spans });
|
2021-05-08 14:57:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
*specified_inline = Some((do_inline, meta.span()));
|
|
|
|
true
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
2021-05-08 14:57:10 +00:00
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
meta.span(),
|
2022-07-11 17:59:04 +00:00
|
|
|
errors::DocInlineOnlyUse {
|
|
|
|
attr_span: meta.span(),
|
|
|
|
item_span: (attr.style == AttrStyle::Outer)
|
|
|
|
.then(|| self.tcx.hir().span(hir_id)),
|
2021-05-08 14:57:10 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:38:09 +00:00
|
|
|
/// Checks that an attribute is *not* used at the crate level. Returns `true` if valid.
|
2021-05-08 14:25:00 +00:00
|
|
|
fn check_attr_not_crate_level(
|
2020-12-01 13:11:33 +00:00
|
|
|
&self,
|
|
|
|
meta: &NestedMetaItem,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr_name: &str,
|
|
|
|
) -> bool {
|
|
|
|
if CRATE_HIR_ID == hir_id {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DocAttrNotCrateLevel { span: meta.span(), attr_name });
|
2020-12-01 13:11:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-12-01 13:19:53 +00:00
|
|
|
true
|
2020-12-01 13:11:33 +00:00
|
|
|
}
|
|
|
|
|
2021-05-10 16:38:09 +00:00
|
|
|
/// Checks that an attribute is used at the crate level. Returns `true` if valid.
|
2021-05-08 14:25:00 +00:00
|
|
|
fn check_attr_crate_level(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
meta: &NestedMetaItem,
|
|
|
|
hir_id: HirId,
|
|
|
|
) -> bool {
|
|
|
|
if hir_id != CRATE_HIR_ID {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.struct_span_lint_hir(INVALID_DOC_ATTRIBUTES, hir_id, meta.span(), |lint| {
|
|
|
|
let mut err = lint.build(fluent::passes::attr_crate_level);
|
|
|
|
if attr.style == AttrStyle::Outer
|
2022-09-20 05:11:23 +00:00
|
|
|
&& self.tcx.hir().get_parent_item(hir_id) == CRATE_OWNER_ID
|
2022-07-11 17:59:04 +00:00
|
|
|
{
|
|
|
|
if let Ok(mut src) = self.tcx.sess.source_map().span_to_snippet(attr.span) {
|
|
|
|
src.insert(1, '!');
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
attr.span,
|
|
|
|
fluent::passes::suggestion,
|
|
|
|
src,
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
err.span_help(attr.span, fluent::passes::help);
|
2021-05-08 14:25:00 +00:00
|
|
|
}
|
2022-07-11 17:59:04 +00:00
|
|
|
}
|
|
|
|
err.note(fluent::passes::note).emit();
|
|
|
|
});
|
2021-05-08 14:25:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-08-03 12:04:27 +00:00
|
|
|
/// Checks that `doc(test(...))` attribute contains only valid attributes. Returns `true` if
|
|
|
|
/// valid.
|
|
|
|
fn check_test_attr(&self, meta: &NestedMetaItem, hir_id: HirId) -> bool {
|
|
|
|
let mut is_valid = true;
|
|
|
|
if let Some(metas) = meta.meta_item_list() {
|
|
|
|
for i_meta in metas {
|
|
|
|
match i_meta.name_or_empty() {
|
|
|
|
sym::attr | sym::no_crate_inject => {}
|
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
2021-08-03 12:04:27 +00:00
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
i_meta.span(),
|
2022-07-11 17:59:04 +00:00
|
|
|
errors::DocTestUnknown {
|
|
|
|
path: rustc_ast_pretty::pprust::path_to_string(
|
|
|
|
&i_meta.meta_item().unwrap().path,
|
|
|
|
),
|
2021-08-03 12:04:27 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
is_valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
meta.span(),
|
|
|
|
errors::DocTestTakesList,
|
|
|
|
);
|
2021-08-03 12:04:27 +00:00
|
|
|
is_valid = false;
|
|
|
|
}
|
|
|
|
is_valid
|
|
|
|
}
|
|
|
|
|
2021-05-10 16:38:09 +00:00
|
|
|
/// Runs various checks on `#[doc]` attributes. Returns `true` if valid.
|
|
|
|
///
|
|
|
|
/// `specified_inline` should be initialized to `None` and kept for the scope
|
|
|
|
/// of one item. Read the documentation of [`check_doc_inline`] for more information.
|
|
|
|
///
|
|
|
|
/// [`check_doc_inline`]: Self::check_doc_inline
|
2021-05-08 14:57:10 +00:00
|
|
|
fn check_doc_attrs(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
hir_id: HirId,
|
|
|
|
target: Target,
|
|
|
|
specified_inline: &mut Option<(bool, Span)>,
|
2021-10-01 18:54:36 +00:00
|
|
|
aliases: &mut FxHashMap<String, Span>,
|
2021-05-08 14:57:10 +00:00
|
|
|
) -> bool {
|
2021-03-11 23:41:51 +00:00
|
|
|
let mut is_valid = true;
|
|
|
|
|
2022-04-05 12:52:53 +00:00
|
|
|
if let Some(mi) = attr.meta() && let Some(list) = mi.meta_item_list() {
|
2022-06-21 11:42:34 +00:00
|
|
|
for meta in list {
|
2021-03-13 21:13:27 +00:00
|
|
|
if let Some(i_meta) = meta.meta_item() {
|
|
|
|
match i_meta.name_or_empty() {
|
|
|
|
sym::alias
|
2022-04-05 12:52:53 +00:00
|
|
|
if !self.check_attr_not_crate_level(meta, hir_id, "alias")
|
|
|
|
|| !self.check_doc_alias(meta, hir_id, target, aliases) =>
|
2020-07-08 12:48:31 +00:00
|
|
|
{
|
2021-03-11 23:41:51 +00:00
|
|
|
is_valid = false
|
2020-09-04 15:12:53 +00:00
|
|
|
}
|
2021-03-13 21:13:27 +00:00
|
|
|
|
|
|
|
sym::keyword
|
2022-04-05 12:52:53 +00:00
|
|
|
if !self.check_attr_not_crate_level(meta, hir_id, "keyword")
|
|
|
|
|| !self.check_doc_keyword(meta, hir_id) =>
|
2020-12-01 13:19:53 +00:00
|
|
|
{
|
2021-03-11 23:41:51 +00:00
|
|
|
is_valid = false
|
2020-12-01 13:19:53 +00:00
|
|
|
}
|
2021-03-13 21:13:27 +00:00
|
|
|
|
2022-06-16 16:50:57 +00:00
|
|
|
sym::fake_variadic
|
|
|
|
if !self.check_attr_not_crate_level(meta, hir_id, "fake_variadic")
|
|
|
|
|| !self.check_doc_fake_variadic(meta, hir_id) =>
|
2022-06-09 02:26:51 +00:00
|
|
|
{
|
|
|
|
is_valid = false
|
|
|
|
}
|
|
|
|
|
2021-05-08 14:25:00 +00:00
|
|
|
sym::html_favicon_url
|
|
|
|
| sym::html_logo_url
|
|
|
|
| sym::html_playground_url
|
|
|
|
| sym::issue_tracker_base_url
|
|
|
|
| sym::html_root_url
|
|
|
|
| sym::html_no_source
|
|
|
|
| sym::test
|
2022-04-05 12:52:53 +00:00
|
|
|
if !self.check_attr_crate_level(attr, meta, hir_id) =>
|
2021-05-08 14:25:00 +00:00
|
|
|
{
|
|
|
|
is_valid = false;
|
|
|
|
}
|
|
|
|
|
2021-05-08 14:57:10 +00:00
|
|
|
sym::inline | sym::no_inline
|
|
|
|
if !self.check_doc_inline(
|
2022-04-05 12:52:53 +00:00
|
|
|
attr,
|
|
|
|
meta,
|
2021-03-02 20:10:05 +00:00
|
|
|
hir_id,
|
2021-05-08 14:57:10 +00:00
|
|
|
target,
|
|
|
|
specified_inline,
|
|
|
|
) =>
|
|
|
|
{
|
2021-03-11 23:41:51 +00:00
|
|
|
is_valid = false;
|
2021-03-02 20:10:05 +00:00
|
|
|
}
|
2021-03-13 21:13:27 +00:00
|
|
|
|
|
|
|
// no_default_passes: deprecated
|
|
|
|
// passes: deprecated
|
|
|
|
// plugins: removed, but rustdoc warns about it itself
|
|
|
|
sym::alias
|
|
|
|
| sym::cfg
|
2021-04-23 14:52:38 +00:00
|
|
|
| sym::cfg_hide
|
2021-03-13 21:13:27 +00:00
|
|
|
| sym::hidden
|
|
|
|
| sym::html_favicon_url
|
|
|
|
| sym::html_logo_url
|
|
|
|
| sym::html_no_source
|
|
|
|
| sym::html_playground_url
|
|
|
|
| sym::html_root_url
|
2021-05-08 14:57:10 +00:00
|
|
|
| sym::inline
|
2021-03-13 21:13:27 +00:00
|
|
|
| sym::issue_tracker_base_url
|
|
|
|
| sym::keyword
|
|
|
|
| sym::masked
|
|
|
|
| sym::no_default_passes
|
2021-05-08 14:57:10 +00:00
|
|
|
| sym::no_inline
|
2021-03-09 03:35:53 +00:00
|
|
|
| sym::notable_trait
|
2021-03-13 21:13:27 +00:00
|
|
|
| sym::passes
|
2022-06-09 02:26:51 +00:00
|
|
|
| sym::plugins
|
2022-06-16 16:50:57 +00:00
|
|
|
| sym::fake_variadic => {}
|
2021-08-03 12:04:27 +00:00
|
|
|
|
|
|
|
sym::test => {
|
2022-04-05 12:52:53 +00:00
|
|
|
if !self.check_test_attr(meta, hir_id) {
|
2021-08-03 12:04:27 +00:00
|
|
|
is_valid = false;
|
|
|
|
}
|
|
|
|
}
|
2021-03-13 21:13:27 +00:00
|
|
|
|
2021-07-11 02:06:10 +00:00
|
|
|
sym::primitive => {
|
2021-10-30 15:44:50 +00:00
|
|
|
if !self.tcx.features().rustdoc_internals {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
2021-07-11 02:06:10 +00:00
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
i_meta.span,
|
2022-07-11 17:59:04 +00:00
|
|
|
errors::DocPrimitive,
|
2021-07-11 02:06:10 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-13 21:13:27 +00:00
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
let path = rustc_ast_pretty::pprust::path_to_string(&i_meta.path);
|
|
|
|
if i_meta.has_name(sym::spotlight) {
|
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
i_meta.span,
|
|
|
|
errors::DocTestUnknownSpotlight {
|
|
|
|
path,
|
|
|
|
span: i_meta.span
|
2021-03-09 03:35:53 +00:00
|
|
|
}
|
2022-07-11 17:59:04 +00:00
|
|
|
);
|
|
|
|
} else if i_meta.has_name(sym::include) &&
|
|
|
|
let Some(value) = i_meta.value_str() {
|
|
|
|
let applicability = if list.len() == 1 {
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
} else {
|
|
|
|
Applicability::MaybeIncorrect
|
|
|
|
};
|
|
|
|
// If there are multiple attributes, the suggestion would suggest
|
|
|
|
// deleting all of them, which is incorrect.
|
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
i_meta.span,
|
|
|
|
errors::DocTestUnknownInclude {
|
|
|
|
path,
|
|
|
|
value: value.to_string(),
|
|
|
|
inner: (attr.style == AttrStyle::Inner)
|
|
|
|
.then_some("!")
|
|
|
|
.unwrap_or(""),
|
|
|
|
sugg: (attr.meta().unwrap().span, applicability),
|
2021-05-19 01:46:41 +00:00
|
|
|
}
|
2022-07-11 17:59:04 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
i_meta.span,
|
|
|
|
errors::DocTestUnknownAny { path }
|
|
|
|
);
|
|
|
|
}
|
2021-03-11 23:41:51 +00:00
|
|
|
is_valid = false;
|
2021-03-01 16:12:39 +00:00
|
|
|
}
|
2020-07-08 12:48:31 +00:00
|
|
|
}
|
2021-03-13 21:25:27 +00:00
|
|
|
} else {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
2021-03-13 21:25:27 +00:00
|
|
|
INVALID_DOC_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
meta.span(),
|
2022-07-11 17:59:04 +00:00
|
|
|
errors::DocInvalid,
|
2021-03-13 21:25:27 +00:00
|
|
|
);
|
2021-03-11 23:41:51 +00:00
|
|
|
is_valid = false;
|
2020-07-08 12:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-11 23:41:51 +00:00
|
|
|
|
|
|
|
is_valid
|
2020-07-08 12:48:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 11:38:16 +00:00
|
|
|
/// Warns against some misuses of `#[pass_by_value]`
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_pass_by_value(&self, attr: &Attribute, span: Span, target: Target) -> bool {
|
2022-01-07 11:38:16 +00:00
|
|
|
match target {
|
2022-01-10 08:54:42 +00:00
|
|
|
Target::Struct | Target::Enum | Target::TyAlias => true,
|
2022-01-07 11:38:16 +00:00
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::PassByValue { attr_span: attr.span, span });
|
2022-01-07 11:38:16 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-15 15:30:30 +00:00
|
|
|
fn check_allow_incoherent_impl(&self, attr: &Attribute, span: Span, target: Target) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::Method(MethodKind::Inherent) => true,
|
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::AllowIncoherentImpl { attr_span: attr.span, span });
|
2022-03-15 15:30:30 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 14:26:30 +00:00
|
|
|
fn check_has_incoherent_inherent_impls(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::Trait | Target::Struct | Target::Enum | Target::Union | Target::ForeignTy => {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
2022-07-11 17:59:04 +00:00
|
|
|
.emit_err(errors::HasIncoherentInherentImpl { attr_span: attr.span, span });
|
2022-04-28 14:26:30 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-06 20:19:39 +00:00
|
|
|
/// Warns against some misuses of `#[must_use]`
|
2022-02-12 00:21:02 +00:00
|
|
|
fn check_must_use(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
|
2021-10-06 20:19:39 +00:00
|
|
|
let node = self.tcx.hir().get(hir_id);
|
2022-02-28 18:52:36 +00:00
|
|
|
if let Some(kind) = node.fn_kind() && let rustc_hir::IsAsync::Async = kind.asyncness() {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::MustUseAsync { span }
|
|
|
|
);
|
2021-10-06 20:19:39 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 00:21:02 +00:00
|
|
|
if !matches!(
|
|
|
|
target,
|
|
|
|
Target::Fn
|
|
|
|
| Target::Enum
|
|
|
|
| Target::Struct
|
|
|
|
| Target::Union
|
|
|
|
| Target::Method(_)
|
|
|
|
| Target::ForeignFn
|
|
|
|
// `impl Trait` in return position can trip
|
|
|
|
// `unused_must_use` if `Trait` is marked as
|
|
|
|
// `#[must_use]`
|
|
|
|
| Target::Trait
|
|
|
|
) {
|
|
|
|
let article = match target {
|
|
|
|
Target::ExternCrate
|
|
|
|
| Target::OpaqueTy
|
|
|
|
| Target::Enum
|
|
|
|
| Target::Impl
|
|
|
|
| Target::Expression
|
|
|
|
| Target::Arm
|
|
|
|
| Target::AssocConst
|
|
|
|
| Target::AssocTy => "an",
|
|
|
|
_ => "a",
|
|
|
|
};
|
|
|
|
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::MustUseNoEffect { article, target },
|
|
|
|
);
|
2022-02-12 00:21:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-06 20:19:39 +00:00
|
|
|
// For now, its always valid
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2021-09-05 02:36:51 +00:00
|
|
|
/// Checks if `#[must_not_suspend]` is applied to a function. Returns `true` if valid.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_must_not_suspend(&self, attr: &Attribute, span: Span, target: Target) -> bool {
|
2021-09-05 02:36:51 +00:00
|
|
|
match target {
|
2021-09-11 19:06:05 +00:00
|
|
|
Target::Struct | Target::Enum | Target::Union | Target::Trait => true,
|
|
|
|
_ => {
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.sess.emit_err(errors::MustNotSuspend { attr_span: attr.span, span });
|
2021-09-05 02:36:51 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 04:47:42 +00:00
|
|
|
/// Checks if `#[cold]` is applied to a non-function. Returns `true` if valid.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_cold(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
|
2020-06-14 04:47:42 +00:00
|
|
|
match target {
|
2020-08-12 16:43:14 +00:00
|
|
|
Target::Fn | Target::Method(..) | Target::ForeignFn | Target::Closure => {}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[cold]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "cold");
|
|
|
|
}
|
2020-06-14 04:47:42 +00:00
|
|
|
_ => {
|
2020-07-11 14:29:35 +00:00
|
|
|
// FIXME: #[cold] was previously allowed on non-functions and some crates used
|
|
|
|
// this, so only emit a warning.
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::Cold { span },
|
|
|
|
);
|
2020-06-14 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 19:28:40 +00:00
|
|
|
/// Checks if `#[link]` is applied to an item other than a foreign module.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_link(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
|
2022-03-14 10:28:34 +00:00
|
|
|
if target == Target::ForeignMod
|
|
|
|
&& let hir::Node::Item(item) = self.tcx.hir().get(hir_id)
|
|
|
|
&& let Item { kind: ItemKind::ForeignMod { abi, .. }, .. } = item
|
|
|
|
&& !matches!(abi, Abi::Rust | Abi::RustIntrinsic | Abi::PlatformIntrinsic)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-01-09 19:28:40 +00:00
|
|
|
|
2022-07-11 17:59:04 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::Link { span: (target != Target::ForeignMod).then_some(span) },
|
|
|
|
);
|
2022-01-09 19:28:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-11 14:29:35 +00:00
|
|
|
/// Checks if `#[link_name]` is applied to an item other than a foreign function or static.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_link_name(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
|
2020-07-11 14:29:35 +00:00
|
|
|
match target {
|
|
|
|
Target::ForeignFn | Target::ForeignStatic => {}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[link_name]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "link_name");
|
|
|
|
}
|
2020-07-11 14:29:35 +00:00
|
|
|
_ => {
|
|
|
|
// FIXME: #[cold] was previously allowed on non-functions/statics and some crates
|
|
|
|
// used this, so only emit a warning.
|
2022-07-19 09:00:16 +00:00
|
|
|
let attr_span = matches!(target, Target::ForeignMod).then_some(attr.span);
|
|
|
|
if let Some(s) = attr.value_str() {
|
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::LinkName { span, attr_span, value: s.as_str() },
|
2020-06-14 04:47:42 +00:00
|
|
|
);
|
2022-07-19 09:00:16 +00:00
|
|
|
} else {
|
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::LinkName { span, attr_span, value: "..." },
|
|
|
|
);
|
|
|
|
};
|
2020-07-11 14:29:35 +00:00
|
|
|
}
|
2020-06-14 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if `#[no_link]` is applied to an `extern crate`. Returns `true` if valid.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_no_link(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) -> bool {
|
2021-02-01 14:35:53 +00:00
|
|
|
match target {
|
|
|
|
Target::ExternCrate => true,
|
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[no_link]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "no_link");
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::NoLink { attr_span: attr.span, span });
|
2021-02-01 14:35:53 +00:00
|
|
|
false
|
|
|
|
}
|
2020-06-14 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 18:41:57 +00:00
|
|
|
fn is_impl_item(&self, hir_id: HirId) -> bool {
|
|
|
|
matches!(self.tcx.hir().get(hir_id), hir::Node::ImplItem(..))
|
|
|
|
}
|
|
|
|
|
2020-06-14 04:47:42 +00:00
|
|
|
/// Checks if `#[export_name]` is applied to a function or static. Returns `true` if valid.
|
2021-02-01 14:35:53 +00:00
|
|
|
fn check_export_name(
|
|
|
|
&self,
|
|
|
|
hir_id: HirId,
|
|
|
|
attr: &Attribute,
|
2022-01-05 12:02:16 +00:00
|
|
|
span: Span,
|
2021-02-01 14:35:53 +00:00
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2020-06-14 04:47:42 +00:00
|
|
|
match target {
|
2021-08-10 18:41:57 +00:00
|
|
|
Target::Static | Target::Fn => true,
|
|
|
|
Target::Method(..) if self.is_impl_item(hir_id) => true,
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[export_name]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "export_name");
|
|
|
|
true
|
|
|
|
}
|
2020-06-14 04:47:42 +00:00
|
|
|
_ => {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::ExportName { attr_span: attr.span, span });
|
2020-06-14 04:47:42 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 00:00:00 +00:00
|
|
|
fn check_rustc_layout_scalar_valid_range(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
2022-01-05 12:02:16 +00:00
|
|
|
span: Span,
|
2021-03-11 00:00:00 +00:00
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
|
|
|
if target != Target::Struct {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::RustcLayoutScalarValidRangeNotStruct {
|
|
|
|
attr_span: attr.span,
|
|
|
|
span,
|
|
|
|
});
|
2021-03-11 00:00:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-18 23:48:49 +00:00
|
|
|
let Some(list) = attr.meta_item_list() else {
|
|
|
|
return false;
|
2021-03-11 00:00:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if matches!(&list[..], &[NestedMetaItem::Literal(Lit { kind: LitKind::Int(..), .. })]) {
|
|
|
|
true
|
|
|
|
} else {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::RustcLayoutScalarValidRangeArg { attr_span: attr.span });
|
2021-03-11 00:00:00 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-23 15:12:28 +00:00
|
|
|
/// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument.
|
|
|
|
fn check_rustc_legacy_const_generics(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
2022-01-05 12:02:16 +00:00
|
|
|
span: Span,
|
2021-02-23 15:12:28 +00:00
|
|
|
target: Target,
|
|
|
|
item: Option<ItemLike<'_>>,
|
|
|
|
) -> bool {
|
2022-03-07 16:31:03 +00:00
|
|
|
let is_function = matches!(target, Target::Fn);
|
2021-02-23 15:12:28 +00:00
|
|
|
if !is_function {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToFn {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2021-02-23 15:12:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-18 23:48:49 +00:00
|
|
|
let Some(list) = attr.meta_item_list() else {
|
2021-02-23 15:12:28 +00:00
|
|
|
// The attribute form is validated on AST.
|
2022-02-18 23:48:49 +00:00
|
|
|
return false;
|
2021-02-23 15:12:28 +00:00
|
|
|
};
|
|
|
|
|
2022-02-18 23:48:49 +00:00
|
|
|
let Some(ItemLike::Item(Item {
|
|
|
|
kind: ItemKind::Fn(FnSig { decl, .. }, generics, _),
|
|
|
|
..
|
|
|
|
})) = item else {
|
|
|
|
bug!("should be a function item");
|
2021-02-25 09:04:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for param in generics.params {
|
|
|
|
match param.kind {
|
|
|
|
hir::GenericParamKind::Const { .. } => {}
|
|
|
|
_ => {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::RustcLegacyConstGenericsOnly {
|
|
|
|
attr_span: attr.span,
|
|
|
|
param_span: param.span,
|
|
|
|
});
|
2021-02-25 09:04:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if list.len() != generics.params.len() {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::RustcLegacyConstGenericsIndex {
|
|
|
|
attr_span: attr.span,
|
|
|
|
generics_span: generics.span,
|
|
|
|
});
|
2021-02-25 09:04:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let arg_count = decl.inputs.len() as u128 + generics.params.len() as u128;
|
2021-02-23 15:12:28 +00:00
|
|
|
let mut invalid_args = vec![];
|
|
|
|
for meta in list {
|
|
|
|
if let Some(LitKind::Int(val, _)) = meta.literal().map(|lit| &lit.kind) {
|
2021-02-25 09:04:43 +00:00
|
|
|
if *val >= arg_count {
|
|
|
|
let span = meta.span();
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::RustcLegacyConstGenericsIndexExceed {
|
|
|
|
span,
|
|
|
|
arg_count: arg_count as usize,
|
|
|
|
});
|
2021-02-25 09:04:43 +00:00
|
|
|
return false;
|
2020-09-29 16:44:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-12-06 00:00:00 +00:00
|
|
|
invalid_args.push(meta.span());
|
2020-09-29 16:44:32 +00:00
|
|
|
}
|
2020-12-06 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !invalid_args.is_empty() {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::RustcLegacyConstGenericsIndexNegative { invalid_args });
|
2020-09-29 16:44:32 +00:00
|
|
|
false
|
2020-12-06 00:00:00 +00:00
|
|
|
} else {
|
|
|
|
true
|
2020-09-29 16:44:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 14:50:06 +00:00
|
|
|
/// Helper function for checking that the provided attribute is only applied to a function or
|
|
|
|
/// method.
|
|
|
|
fn check_applied_to_fn_or_method(&self, attr: &Attribute, span: Span, target: Target) -> bool {
|
2022-01-05 12:02:16 +00:00
|
|
|
let is_function = matches!(target, Target::Fn | Target::Method(..));
|
|
|
|
if !is_function {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::AttrShouldBeAppliedToFn {
|
|
|
|
attr_span: attr.span,
|
|
|
|
defn_span: span,
|
|
|
|
});
|
2022-01-05 12:02:16 +00:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 14:50:06 +00:00
|
|
|
/// Checks that the `#[rustc_lint_query_instability]` attribute is only applied to a function
|
|
|
|
/// or method.
|
|
|
|
fn check_rustc_lint_query_instability(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
|
|
|
self.check_applied_to_fn_or_method(attr, span, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that the `#[rustc_lint_diagnostics]` attribute is only applied to a function or
|
|
|
|
/// method.
|
|
|
|
fn check_rustc_lint_diagnostics(&self, attr: &Attribute, span: Span, target: Target) -> bool {
|
|
|
|
self.check_applied_to_fn_or_method(attr, span, target)
|
|
|
|
}
|
|
|
|
|
2022-07-25 12:02:39 +00:00
|
|
|
/// Checks that the `#[rustc_lint_opt_ty]` attribute is only applied to a struct.
|
|
|
|
fn check_rustc_lint_opt_ty(&self, attr: &Attribute, span: Span, target: Target) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::Struct => true,
|
|
|
|
_ => {
|
|
|
|
self.tcx.sess.emit_err(errors::RustcLintOptTy { attr_span: attr.span, span });
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks that the `#[rustc_lint_opt_deny_field_access]` attribute is only applied to a field.
|
|
|
|
fn check_rustc_lint_opt_deny_field_access(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::Field => true,
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.emit_err(errors::RustcLintOptDenyFieldAccess { attr_span: attr.span, span });
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-23 11:59:53 +00:00
|
|
|
/// Checks that the dep-graph debugging attributes are only present when the query-dep-graph
|
|
|
|
/// option is passed to the compiler.
|
2021-03-22 18:21:55 +00:00
|
|
|
fn check_rustc_dirty_clean(&self, attr: &Attribute) -> bool {
|
2022-07-06 12:44:47 +00:00
|
|
|
if self.tcx.sess.opts.unstable_opts.query_dep_graph {
|
2021-03-22 18:21:55 +00:00
|
|
|
true
|
|
|
|
} else {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::RustcDirtyClean { span: attr.span });
|
2021-03-22 18:21:55 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-11 14:29:35 +00:00
|
|
|
/// Checks if `#[link_section]` is applied to a function or static.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_link_section(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
|
2020-06-14 04:47:42 +00:00
|
|
|
match target {
|
2020-07-11 14:29:35 +00:00
|
|
|
Target::Static | Target::Fn | Target::Method(..) => {}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[link_section]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "link_section");
|
|
|
|
}
|
2020-06-14 04:47:42 +00:00
|
|
|
_ => {
|
2020-07-11 14:29:35 +00:00
|
|
|
// FIXME: #[link_section] was previously allowed on non-functions/statics and some
|
|
|
|
// crates used this, so only emit a warning.
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::LinkSection { span },
|
|
|
|
);
|
2020-06-14 04:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 02:12:24 +00:00
|
|
|
/// Checks if `#[no_mangle]` is applied to a function or static.
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_no_mangle(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
|
2020-09-06 02:12:24 +00:00
|
|
|
match target {
|
2021-08-10 18:41:57 +00:00
|
|
|
Target::Static | Target::Fn => {}
|
|
|
|
Target::Method(..) if self.is_impl_item(hir_id) => {}
|
2021-02-01 14:35:53 +00:00
|
|
|
// FIXME(#80564): We permit struct fields, match arms and macro defs to have an
|
|
|
|
// `#[no_mangle]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "no_mangle");
|
|
|
|
}
|
2021-08-30 00:08:40 +00:00
|
|
|
// FIXME: #[no_mangle] was previously allowed on non-functions/statics, this should be an error
|
|
|
|
// The error should specify that the item that is wrong is specifically a *foreign* fn/static
|
|
|
|
// otherwise the error seems odd
|
|
|
|
Target::ForeignFn | Target::ForeignStatic => {
|
|
|
|
let foreign_item_kind = match target {
|
|
|
|
Target::ForeignFn => "function",
|
|
|
|
Target::ForeignStatic => "static",
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::NoMangleForeign { span, attr_span: attr.span, foreign_item_kind },
|
|
|
|
);
|
2021-08-30 00:08:40 +00:00
|
|
|
}
|
2020-09-06 02:12:24 +00:00
|
|
|
_ => {
|
|
|
|
// FIXME: #[no_mangle] was previously allowed on non-functions/statics and some
|
|
|
|
// crates used this, so only emit a warning.
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::NoMangle { span },
|
|
|
|
);
|
2020-09-06 02:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Checks if the `#[repr]` attributes on `item` are valid.
|
2019-10-11 01:33:16 +00:00
|
|
|
fn check_repr(
|
|
|
|
&self,
|
2021-12-14 05:16:36 +00:00
|
|
|
attrs: &[Attribute],
|
2022-01-05 12:02:16 +00:00
|
|
|
span: Span,
|
2019-10-11 01:33:16 +00:00
|
|
|
target: Target,
|
2020-09-29 16:44:32 +00:00
|
|
|
item: Option<ItemLike<'_>>,
|
2020-01-27 23:27:57 +00:00
|
|
|
hir_id: HirId,
|
2019-10-11 01:33:16 +00:00
|
|
|
) {
|
2018-01-01 20:42:12 +00:00
|
|
|
// Extract the names of all repr hints, e.g., [foo, bar, align] for:
|
|
|
|
// ```
|
|
|
|
// #[repr(foo)]
|
|
|
|
// #[repr(bar, align(8))]
|
|
|
|
// ```
|
2019-10-11 01:33:16 +00:00
|
|
|
let hints: Vec<_> = attrs
|
2018-01-01 20:42:12 +00:00
|
|
|
.iter()
|
2021-04-11 00:00:00 +00:00
|
|
|
.filter(|attr| attr.has_name(sym::repr))
|
2018-01-01 20:42:12 +00:00
|
|
|
.filter_map(|attr| attr.meta_item_list())
|
2018-09-12 10:31:11 +00:00
|
|
|
.flatten()
|
2018-01-01 20:42:12 +00:00
|
|
|
.collect();
|
2016-08-20 01:58:14 +00:00
|
|
|
|
2017-11-20 17:26:54 +00:00
|
|
|
let mut int_reprs = 0;
|
|
|
|
let mut is_c = false;
|
|
|
|
let mut is_simd = false;
|
2018-01-03 16:43:30 +00:00
|
|
|
let mut is_transparent = false;
|
2017-01-14 22:49:29 +00:00
|
|
|
|
2018-01-01 20:42:12 +00:00
|
|
|
for hint in &hints {
|
2021-01-21 02:49:04 +00:00
|
|
|
if !hint.is_meta_item() {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::ReprIdent { span: hint.span() });
|
2021-01-21 02:49:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-05-08 04:33:06 +00:00
|
|
|
let (article, allowed_targets) = match hint.name_or_empty() {
|
2021-01-21 02:49:04 +00:00
|
|
|
sym::C => {
|
|
|
|
is_c = true;
|
2019-05-22 14:31:09 +00:00
|
|
|
match target {
|
|
|
|
Target::Struct | Target::Union | Target::Enum => continue,
|
|
|
|
_ => ("a", "struct, enum, or union"),
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-21 02:49:04 +00:00
|
|
|
sym::align => {
|
2022-05-04 09:18:37 +00:00
|
|
|
if let (Target::Fn, false) = (target, self.tcx.features().fn_align) {
|
2021-01-21 02:49:04 +00:00
|
|
|
feature_err(
|
|
|
|
&self.tcx.sess.parse_sess,
|
|
|
|
sym::fn_align,
|
|
|
|
hint.span(),
|
|
|
|
"`repr(align)` attributes on functions are unstable",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
match target {
|
|
|
|
Target::Struct | Target::Union | Target::Enum | Target::Fn => continue,
|
|
|
|
_ => ("a", "struct, enum, function, or union"),
|
|
|
|
}
|
|
|
|
}
|
2019-05-08 04:33:06 +00:00
|
|
|
sym::packed => {
|
2016-08-10 18:00:17 +00:00
|
|
|
if target != Target::Struct && target != Target::Union {
|
2018-01-01 20:42:12 +00:00
|
|
|
("a", "struct or union")
|
2016-07-03 03:24:27 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2019-05-08 04:33:06 +00:00
|
|
|
sym::simd => {
|
2017-11-20 17:26:54 +00:00
|
|
|
is_simd = true;
|
2020-07-11 14:29:35 +00:00
|
|
|
if target != Target::Struct {
|
|
|
|
("a", "struct")
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2019-05-08 04:33:06 +00:00
|
|
|
sym::transparent => {
|
2018-01-03 16:43:30 +00:00
|
|
|
is_transparent = true;
|
2019-05-22 14:31:09 +00:00
|
|
|
match target {
|
|
|
|
Target::Struct | Target::Union | Target::Enum => continue,
|
|
|
|
_ => ("a", "struct, enum, or union"),
|
2018-01-03 16:43:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-08 04:33:06 +00:00
|
|
|
sym::i8
|
|
|
|
| sym::u8
|
|
|
|
| sym::i16
|
|
|
|
| sym::u16
|
|
|
|
| sym::i32
|
|
|
|
| sym::u32
|
|
|
|
| sym::i64
|
|
|
|
| sym::u64
|
2020-07-06 19:42:19 +00:00
|
|
|
| sym::i128
|
|
|
|
| sym::u128
|
2019-05-08 04:33:06 +00:00
|
|
|
| sym::isize
|
|
|
|
| sym::usize => {
|
2017-11-20 17:26:54 +00:00
|
|
|
int_reprs += 1;
|
2020-07-11 14:29:35 +00:00
|
|
|
if target != Target::Enum {
|
|
|
|
("an", "enum")
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2021-01-21 02:49:04 +00:00
|
|
|
_ => {
|
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
hint.span(),
|
|
|
|
E0552,
|
|
|
|
"unrecognized representation hint"
|
|
|
|
)
|
2022-09-16 03:46:47 +00:00
|
|
|
.help("valid reprs are `C`, `align`, `packed`, `transparent`, `simd`, `i8`, `u8`, \
|
|
|
|
`i16`, `u16`, `i32`, `u32`, `i64`, `u64`, `i128`, `u128`, `isize`, `usize`")
|
2021-01-21 02:49:04 +00:00
|
|
|
.emit();
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2015-11-09 16:43:55 +00:00
|
|
|
};
|
2020-09-10 18:54:17 +00:00
|
|
|
|
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
2019-03-03 17:56:24 +00:00
|
|
|
hint.span(),
|
2020-09-10 18:54:17 +00:00
|
|
|
E0517,
|
|
|
|
"{}",
|
2022-03-18 13:48:21 +00:00
|
|
|
&format!("attribute should be applied to {article} {allowed_targets}")
|
2018-03-22 15:57:26 +00:00
|
|
|
)
|
2022-03-18 13:48:21 +00:00
|
|
|
.span_label(span, &format!("not {article} {allowed_targets}"))
|
2020-09-10 18:54:17 +00:00
|
|
|
.emit();
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2017-11-20 17:26:54 +00:00
|
|
|
|
2018-01-03 16:43:30 +00:00
|
|
|
// Just point at all repr hints if there are any incompatibilities.
|
|
|
|
// This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
|
2019-03-03 17:56:24 +00:00
|
|
|
let hint_spans = hints.iter().map(|hint| hint.span());
|
2018-01-03 16:43:30 +00:00
|
|
|
|
2022-07-07 10:46:22 +00:00
|
|
|
// Error on repr(transparent, <anything else>).
|
|
|
|
if is_transparent && hints.len() > 1 {
|
2018-01-03 16:43:30 +00:00
|
|
|
let hint_spans: Vec<_> = hint_spans.clone().collect();
|
2019-12-31 20:25:16 +00:00
|
|
|
struct_span_err!(
|
2018-01-03 16:43:30 +00:00
|
|
|
self.tcx.sess,
|
|
|
|
hint_spans,
|
|
|
|
E0692,
|
2019-05-22 14:31:09 +00:00
|
|
|
"transparent {} cannot have other repr hints",
|
|
|
|
target
|
2019-12-31 20:25:16 +00:00
|
|
|
)
|
|
|
|
.emit();
|
2018-01-03 16:43:30 +00:00
|
|
|
}
|
2017-11-20 17:26:54 +00:00
|
|
|
// Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
|
|
|
|
if (int_reprs > 1)
|
|
|
|
|| (is_simd && is_c)
|
2020-09-29 16:44:32 +00:00
|
|
|
|| (int_reprs == 1
|
|
|
|
&& is_c
|
|
|
|
&& item.map_or(false, |item| {
|
|
|
|
if let ItemLike::Item(item) = item {
|
|
|
|
return is_c_like_enum(item);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}))
|
2019-10-13 15:14:59 +00:00
|
|
|
{
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
2020-02-01 23:47:58 +00:00
|
|
|
CONFLICTING_REPR_HINTS,
|
|
|
|
hir_id,
|
|
|
|
hint_spans.collect::<Vec<Span>>(),
|
2022-07-19 09:00:16 +00:00
|
|
|
errors::ReprConflicting,
|
2020-02-01 23:47:58 +00:00
|
|
|
);
|
2016-07-03 03:24:27 +00:00
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2018-03-22 15:57:26 +00:00
|
|
|
|
2021-12-14 05:16:36 +00:00
|
|
|
fn check_used(&self, attrs: &[Attribute], target: Target) {
|
2022-02-08 22:51:17 +00:00
|
|
|
let mut used_linker_span = None;
|
|
|
|
let mut used_compiler_span = None;
|
2022-02-25 21:52:17 +00:00
|
|
|
for attr in attrs.iter().filter(|attr| attr.has_name(sym::used)) {
|
|
|
|
if target != Target::Static {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::UsedStatic { span: attr.span });
|
2018-04-30 05:43:22 +00:00
|
|
|
}
|
2022-02-08 22:51:17 +00:00
|
|
|
let inner = attr.meta_item_list();
|
|
|
|
match inner.as_deref() {
|
|
|
|
Some([item]) if item.has_name(sym::linker) => {
|
|
|
|
if used_linker_span.is_none() {
|
|
|
|
used_linker_span = Some(attr.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some([item]) if item.has_name(sym::compiler) => {
|
|
|
|
if used_compiler_span.is_none() {
|
|
|
|
used_compiler_span = Some(attr.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(_) => {
|
2022-09-26 11:00:29 +00:00
|
|
|
// This error case is handled in rustc_hir_analysis::collect.
|
2022-02-08 22:51:17 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
// Default case (compiler) when arg isn't defined.
|
|
|
|
if used_compiler_span.is_none() {
|
|
|
|
used_compiler_span = Some(attr.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let (Some(linker_span), Some(compiler_span)) = (used_linker_span, used_compiler_span) {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
2022-07-19 09:00:16 +00:00
|
|
|
.emit_err(errors::UsedCompilerLinker { spans: vec![linker_span, compiler_span] });
|
2018-04-30 05:43:22 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-21 20:49:08 +00:00
|
|
|
|
|
|
|
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
|
|
|
|
/// (Allows proc_macro functions)
|
|
|
|
fn check_allow_internal_unstable(
|
|
|
|
&self,
|
2021-02-01 14:35:53 +00:00
|
|
|
hir_id: HirId,
|
2020-10-21 20:49:08 +00:00
|
|
|
attr: &Attribute,
|
2022-01-05 12:02:16 +00:00
|
|
|
span: Span,
|
2020-10-21 20:49:08 +00:00
|
|
|
target: Target,
|
|
|
|
attrs: &[Attribute],
|
|
|
|
) -> bool {
|
|
|
|
debug!("Checking target: {:?}", target);
|
2021-02-01 14:35:53 +00:00
|
|
|
match target {
|
|
|
|
Target::Fn => {
|
|
|
|
for attr in attrs {
|
|
|
|
if self.tcx.sess.is_proc_macro_attr(attr) {
|
|
|
|
debug!("Is proc macro attr");
|
|
|
|
return true;
|
|
|
|
}
|
2020-10-21 20:49:08 +00:00
|
|
|
}
|
2021-02-01 14:35:53 +00:00
|
|
|
debug!("Is not proc macro attr");
|
|
|
|
false
|
|
|
|
}
|
|
|
|
Target::MacroDef => true,
|
|
|
|
// FIXME(#80564): We permit struct fields and match arms to have an
|
|
|
|
// `#[allow_internal_unstable]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm => {
|
|
|
|
self.inline_attr_str_error_without_macro_def(
|
|
|
|
hir_id,
|
|
|
|
attr,
|
|
|
|
"allow_internal_unstable",
|
|
|
|
);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
2022-07-19 09:00:16 +00:00
|
|
|
.emit_err(errors::AllowInternalUnstable { attr_span: attr.span, span });
|
2021-02-01 14:35:53 +00:00
|
|
|
false
|
2020-10-21 20:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-21 22:02:26 +00:00
|
|
|
|
2022-04-26 01:02:43 +00:00
|
|
|
/// Checks if the items on the `#[debugger_visualizer]` attribute are valid.
|
|
|
|
fn check_debugger_visualizer(&self, attr: &Attribute, target: Target) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::Mod => {}
|
|
|
|
_ => {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DebugVisualizerPlacement { span: attr.span });
|
2022-04-26 01:02:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 18:14:48 +00:00
|
|
|
let Some(hints) = attr.meta_item_list() else {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DebugVisualizerInvalid { span: attr.span });
|
2022-05-24 18:14:48 +00:00
|
|
|
return false;
|
2022-04-26 01:02:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let hint = match hints.len() {
|
|
|
|
1 => &hints[0],
|
|
|
|
_ => {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DebugVisualizerInvalid { span: attr.span });
|
2022-04-26 01:02:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-24 18:14:48 +00:00
|
|
|
let Some(meta_item) = hint.meta_item() else {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DebugVisualizerInvalid { span: attr.span });
|
2022-04-26 01:02:43 +00:00
|
|
|
return false;
|
2022-05-24 18:14:48 +00:00
|
|
|
};
|
2022-04-26 01:02:43 +00:00
|
|
|
|
2022-05-24 18:14:48 +00:00
|
|
|
let visualizer_path = match (meta_item.name_or_empty(), meta_item.value_str()) {
|
|
|
|
(sym::natvis_file, Some(value)) => value,
|
|
|
|
(sym::gdb_script_file, Some(value)) => value,
|
|
|
|
(_, _) => {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::DebugVisualizerInvalid { span: meta_item.span });
|
2022-04-26 01:02:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-24 18:14:48 +00:00
|
|
|
let file =
|
|
|
|
match resolve_path(&self.tcx.sess.parse_sess, visualizer_path.as_str(), attr.span) {
|
|
|
|
Ok(file) => file,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match std::fs::File::open(&file) {
|
|
|
|
Ok(_) => true,
|
|
|
|
Err(err) => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
meta_item.span,
|
|
|
|
&format!("couldn't read {}: {}", file.display(), err),
|
|
|
|
)
|
|
|
|
.emit();
|
2022-04-26 01:02:43 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 22:02:26 +00:00
|
|
|
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
|
|
|
|
/// (Allows proc_macro functions)
|
|
|
|
fn check_rustc_allow_const_fn_unstable(
|
|
|
|
&self,
|
2020-10-23 15:54:48 +00:00
|
|
|
hir_id: HirId,
|
2020-10-21 22:02:26 +00:00
|
|
|
attr: &Attribute,
|
2022-01-05 12:02:16 +00:00
|
|
|
span: Span,
|
2020-10-21 22:02:26 +00:00
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
2021-02-01 14:35:53 +00:00
|
|
|
match target {
|
|
|
|
Target::Fn | Target::Method(_)
|
2022-03-13 10:12:50 +00:00
|
|
|
if self.tcx.is_const_fn_raw(self.tcx.hir().local_def_id(hir_id).to_def_id()) =>
|
2021-02-01 14:35:53 +00:00
|
|
|
{
|
|
|
|
true
|
|
|
|
}
|
|
|
|
// FIXME(#80564): We permit struct fields and match arms to have an
|
|
|
|
// `#[allow_internal_unstable]` attribute with just a lint, because we previously
|
|
|
|
// erroneously allowed it and some crates used it accidentally, to to be compatible
|
|
|
|
// with crates depending on them, we can't throw an error here.
|
|
|
|
Target::Field | Target::Arm | Target::MacroDef => {
|
|
|
|
self.inline_attr_str_error_with_macro_def(hir_id, attr, "allow_internal_unstable");
|
|
|
|
true
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
2022-07-19 09:00:16 +00:00
|
|
|
.emit_err(errors::RustcAllowConstFnUnstable { attr_span: attr.span, span });
|
2021-02-01 14:35:53 +00:00
|
|
|
false
|
2020-10-23 15:54:48 +00:00
|
|
|
}
|
2020-10-21 22:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-10 03:13:52 +00:00
|
|
|
|
2022-05-04 09:18:37 +00:00
|
|
|
fn check_rustc_std_internal_symbol(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
span: Span,
|
|
|
|
target: Target,
|
|
|
|
) -> bool {
|
|
|
|
match target {
|
|
|
|
Target::Fn | Target::Static => true,
|
|
|
|
_ => {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
2022-07-19 09:00:16 +00:00
|
|
|
.emit_err(errors::RustcStdInternalSymbol { attr_span: attr.span, span });
|
2022-05-04 09:18:37 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 09:49:54 +00:00
|
|
|
/// `#[const_trait]` only applies to traits.
|
|
|
|
fn check_const_trait(&self, attr: &Attribute, _span: Span, target: Target) -> bool {
|
2021-07-10 03:13:52 +00:00
|
|
|
match target {
|
2022-03-16 09:49:54 +00:00
|
|
|
Target::Trait => true,
|
2021-07-10 03:13:52 +00:00
|
|
|
_ => {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::ConstTrait { attr_span: attr.span });
|
2021-07-10 03:13:52 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-29 17:00:41 +00:00
|
|
|
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_stability_promotable(&self, attr: &Attribute, _span: Span, target: Target) -> bool {
|
2021-07-29 17:00:41 +00:00
|
|
|
match target {
|
|
|
|
Target::Expression => {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.sess.emit_err(errors::StabilityPromotable { attr_span: attr.span });
|
2021-07-29 17:00:41 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 03:30:27 +00:00
|
|
|
fn check_link_ordinal(&self, attr: &Attribute, _span: Span, target: Target) -> bool {
|
|
|
|
match target {
|
2022-08-04 01:28:59 +00:00
|
|
|
Target::ForeignFn | Target::ForeignStatic => true,
|
2022-08-03 03:30:27 +00:00
|
|
|
_ => {
|
|
|
|
self.tcx.sess.emit_err(errors::LinkOrdinal { attr_span: attr.span });
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 12:02:16 +00:00
|
|
|
fn check_deprecated(&self, hir_id: HirId, attr: &Attribute, _span: Span, target: Target) {
|
2021-07-29 17:00:41 +00:00
|
|
|
match target {
|
|
|
|
Target::Closure | Target::Expression | Target::Statement | Target::Arm => {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::Deprecated,
|
|
|
|
);
|
2021-07-29 17:00:41 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_macro_use(&self, hir_id: HirId, attr: &Attribute, target: Target) {
|
|
|
|
let name = attr.name_or_empty();
|
|
|
|
match target {
|
|
|
|
Target::ExternCrate | Target::Mod => {}
|
|
|
|
_ => {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::MacroUse { name },
|
|
|
|
);
|
2021-07-29 17:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_macro_export(&self, hir_id: HirId, attr: &Attribute, target: Target) {
|
|
|
|
if target != Target::MacroDef {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.emit_spanned_lint(UNUSED_ATTRIBUTES, hir_id, attr.span, errors::MacroExport);
|
2021-07-29 17:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_plugin_registrar(&self, hir_id: HirId, attr: &Attribute, target: Target) {
|
|
|
|
if target != Target::Fn {
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::PluginRegistrar,
|
|
|
|
);
|
2021-07-29 17:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-03 21:28:56 +00:00
|
|
|
|
|
|
|
fn check_unused_attribute(&self, hir_id: HirId, attr: &Attribute) {
|
|
|
|
// Warn on useless empty attributes.
|
|
|
|
let note = if matches!(
|
|
|
|
attr.name_or_empty(),
|
|
|
|
sym::macro_use
|
|
|
|
| sym::allow
|
|
|
|
| sym::expect
|
|
|
|
| sym::warn
|
|
|
|
| sym::deny
|
|
|
|
| sym::forbid
|
|
|
|
| sym::feature
|
|
|
|
| sym::repr
|
|
|
|
| sym::target_feature
|
|
|
|
) && attr.meta_item_list().map_or(false, |list| list.is_empty())
|
|
|
|
{
|
2022-07-19 09:00:16 +00:00
|
|
|
errors::UnusedNote::EmptyList { name: attr.name_or_empty() }
|
2022-03-03 21:28:56 +00:00
|
|
|
} else if matches!(
|
|
|
|
attr.name_or_empty(),
|
|
|
|
sym::allow | sym::warn | sym::deny | sym::forbid | sym::expect
|
|
|
|
) && let Some(meta) = attr.meta_item_list()
|
|
|
|
&& meta.len() == 1
|
|
|
|
&& let Some(item) = meta[0].meta_item()
|
|
|
|
&& let MetaItemKind::NameValue(_) = &item.kind
|
|
|
|
&& item.path == sym::reason
|
|
|
|
{
|
2022-07-19 09:00:16 +00:00
|
|
|
errors::UnusedNote::NoLints { name: attr.name_or_empty() }
|
2022-03-16 09:49:54 +00:00
|
|
|
} else if attr.name_or_empty() == sym::default_method_body_is_const {
|
2022-07-19 09:00:16 +00:00
|
|
|
errors::UnusedNote::DefaultMethodBodyConst
|
2022-03-03 21:28:56 +00:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2022-07-19 09:00:16 +00:00
|
|
|
self.tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
attr.span,
|
|
|
|
errors::Unused { attr_span: attr.span, note },
|
|
|
|
);
|
2022-03-03 21:28:56 +00:00
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 05:16:36 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
|
2021-11-03 23:03:12 +00:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2020-01-07 16:25:33 +00:00
|
|
|
|
2021-11-03 23:03:12 +00:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.tcx.hir()
|
2018-01-08 21:43:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 18:28:50 +00:00
|
|
|
fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
|
2021-07-31 06:50:57 +00:00
|
|
|
// Historically we've run more checks on non-exported than exported macros,
|
|
|
|
// so this lets us continue to run them while maintaining backwards compatibility.
|
|
|
|
// In the long run, the checks should be harmonized.
|
2021-12-11 11:52:23 +00:00
|
|
|
if let ItemKind::Macro(ref macro_def, _) = item.kind {
|
2021-07-31 06:50:57 +00:00
|
|
|
let def_id = item.def_id.to_def_id();
|
|
|
|
if macro_def.macro_rules && !self.tcx.has_attr(def_id, sym::macro_export) {
|
|
|
|
check_non_exported_macro_for_invalid_attrs(self.tcx, item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 06:25:59 +00:00
|
|
|
let target = Target::from_item(item);
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_attributes(item.hir_id(), item.span, target, Some(ItemLike::Item(item)));
|
2018-03-22 15:57:26 +00:00
|
|
|
intravisit::walk_item(self, item)
|
|
|
|
}
|
|
|
|
|
2020-11-15 13:03:30 +00:00
|
|
|
fn visit_generic_param(&mut self, generic_param: &'tcx hir::GenericParam<'tcx>) {
|
|
|
|
let target = Target::from_generic_param(generic_param);
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_attributes(generic_param.hir_id, generic_param.span, target, None);
|
2020-11-15 13:03:30 +00:00
|
|
|
intravisit::walk_generic_param(self, generic_param)
|
|
|
|
}
|
|
|
|
|
2019-11-28 20:47:10 +00:00
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) {
|
2019-10-11 01:35:22 +00:00
|
|
|
let target = Target::from_trait_item(trait_item);
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_attributes(trait_item.hir_id(), trait_item.span, target, None);
|
2019-10-11 01:35:22 +00:00
|
|
|
intravisit::walk_trait_item(self, trait_item)
|
|
|
|
}
|
2018-03-22 15:57:26 +00:00
|
|
|
|
2021-03-15 21:36:07 +00:00
|
|
|
fn visit_field_def(&mut self, struct_field: &'tcx hir::FieldDef<'tcx>) {
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_attributes(struct_field.hir_id, struct_field.span, Target::Field, None);
|
2021-03-15 21:36:07 +00:00
|
|
|
intravisit::walk_field_def(self, struct_field);
|
2021-02-01 14:35:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) {
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_attributes(arm.hir_id, arm.span, Target::Arm, None);
|
2021-02-01 14:35:53 +00:00
|
|
|
intravisit::walk_arm(self, arm);
|
|
|
|
}
|
|
|
|
|
2020-09-29 16:44:32 +00:00
|
|
|
fn visit_foreign_item(&mut self, f_item: &'tcx ForeignItem<'tcx>) {
|
2019-10-11 01:35:49 +00:00
|
|
|
let target = Target::from_foreign_item(f_item);
|
2022-07-25 20:36:03 +00:00
|
|
|
self.check_attributes(f_item.hir_id(), f_item.span, target, Some(ItemLike::ForeignItem));
|
2019-10-11 01:35:49 +00:00
|
|
|
intravisit::walk_foreign_item(self, f_item)
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:16:44 +00:00
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
|
2020-01-27 18:37:36 +00:00
|
|
|
let target = target_from_impl_item(self.tcx, impl_item);
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_attributes(impl_item.hir_id(), impl_item.span, target, None);
|
2019-10-13 15:14:59 +00:00
|
|
|
intravisit::walk_impl_item(self, impl_item)
|
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
|
2020-09-10 18:54:17 +00:00
|
|
|
// When checking statements ignore expressions, they will be checked later.
|
|
|
|
if let hir::StmtKind::Local(ref l) = stmt.kind {
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_attributes(l.hir_id, stmt.span, Target::Statement, None);
|
2020-09-10 18:54:17 +00:00
|
|
|
}
|
2018-03-22 15:57:26 +00:00
|
|
|
intravisit::walk_stmt(self, stmt)
|
|
|
|
}
|
|
|
|
|
2019-11-29 12:43:03 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
2020-09-10 18:54:17 +00:00
|
|
|
let target = match expr.kind {
|
2022-06-11 19:25:25 +00:00
|
|
|
hir::ExprKind::Closure { .. } => Target::Closure,
|
2020-09-10 18:54:17 +00:00
|
|
|
_ => Target::Expression,
|
|
|
|
};
|
|
|
|
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_attributes(expr.hir_id, expr.span, target, None);
|
2018-03-22 15:57:26 +00:00
|
|
|
intravisit::walk_expr(self, expr)
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
2020-09-10 18:54:17 +00:00
|
|
|
|
2022-05-23 01:34:37 +00:00
|
|
|
fn visit_expr_field(&mut self, field: &'tcx hir::ExprField<'tcx>) {
|
|
|
|
self.check_attributes(field.hir_id, field.span, Target::ExprField, None);
|
|
|
|
intravisit::walk_expr_field(self, field)
|
|
|
|
}
|
|
|
|
|
2022-08-10 01:22:01 +00:00
|
|
|
fn visit_variant(&mut self, variant: &'tcx hir::Variant<'tcx>) {
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_attributes(variant.id, variant.span, Target::Variant, None);
|
2022-08-10 01:22:01 +00:00
|
|
|
intravisit::walk_variant(self, variant)
|
2020-09-10 18:54:17 +00:00
|
|
|
}
|
2021-01-08 09:00:39 +00:00
|
|
|
|
|
|
|
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
|
2022-01-05 12:02:16 +00:00
|
|
|
self.check_attributes(param.hir_id, param.span, Target::Param, None);
|
2021-01-08 09:00:39 +00:00
|
|
|
|
|
|
|
intravisit::walk_param(self, param);
|
|
|
|
}
|
2022-05-03 18:52:53 +00:00
|
|
|
|
2022-05-23 01:34:37 +00:00
|
|
|
fn visit_pat_field(&mut self, field: &'tcx hir::PatField<'tcx>) {
|
|
|
|
self.check_attributes(field.hir_id, field.span, Target::PatField, None);
|
|
|
|
intravisit::walk_pat_field(self, field);
|
2022-05-03 18:52:53 +00:00
|
|
|
}
|
2015-09-25 06:25:59 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 18:28:50 +00:00
|
|
|
fn is_c_like_enum(item: &Item<'_>) -> bool {
|
2019-10-11 01:33:16 +00:00
|
|
|
if let ItemKind::Enum(ref def, _) = item.kind {
|
2019-11-29 08:26:18 +00:00
|
|
|
for variant in def.variants {
|
2019-08-14 00:40:21 +00:00
|
|
|
match variant.data {
|
2019-02-02 14:40:08 +00:00
|
|
|
hir::VariantData::Unit(..) => { /* continue */ }
|
2019-10-11 01:33:16 +00:00
|
|
|
_ => return false,
|
2017-11-20 17:26:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2018-06-06 20:13:52 +00:00
|
|
|
|
2021-10-09 12:13:15 +00:00
|
|
|
// FIXME: Fix "Cannot determine resolution" error and remove built-in macros
|
|
|
|
// from this check.
|
2020-10-03 18:45:39 +00:00
|
|
|
fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
|
2021-10-09 12:13:15 +00:00
|
|
|
// Check for builtin attributes at the crate level
|
|
|
|
// which were unsuccessfully resolved due to cannot determine
|
|
|
|
// resolution for the attribute macro error.
|
2020-10-03 18:45:39 +00:00
|
|
|
const ATTRS_TO_CHECK: &[Symbol] = &[
|
|
|
|
sym::macro_export,
|
|
|
|
sym::repr,
|
|
|
|
sym::path,
|
|
|
|
sym::automatically_derived,
|
|
|
|
sym::start,
|
2021-04-08 13:37:38 +00:00
|
|
|
sym::rustc_main,
|
2022-07-05 17:56:22 +00:00
|
|
|
sym::unix_sigpipe,
|
2021-10-09 12:13:15 +00:00
|
|
|
sym::derive,
|
|
|
|
sym::test,
|
|
|
|
sym::test_case,
|
|
|
|
sym::global_allocator,
|
|
|
|
sym::bench,
|
2020-10-03 18:45:39 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for attr in attrs {
|
2021-10-09 12:13:15 +00:00
|
|
|
// This function should only be called with crate attributes
|
|
|
|
// which are inner attributes always but lets check to make sure
|
|
|
|
if attr.style == AttrStyle::Inner {
|
|
|
|
for attr_to_check in ATTRS_TO_CHECK {
|
|
|
|
if attr.has_name(*attr_to_check) {
|
|
|
|
let mut err = tcx.sess.struct_span_err(
|
2020-10-03 18:45:39 +00:00
|
|
|
attr.span,
|
|
|
|
&format!(
|
|
|
|
"`{}` attribute cannot be used at crate level",
|
|
|
|
attr_to_check.to_ident_string()
|
|
|
|
),
|
2021-10-09 12:13:15 +00:00
|
|
|
);
|
|
|
|
// Only emit an error with a suggestion if we can create a
|
|
|
|
// string out of the attribute span
|
|
|
|
if let Ok(src) = tcx.sess.source_map().span_to_snippet(attr.span) {
|
|
|
|
let replacement = src.replace("#!", "#");
|
|
|
|
err.span_suggestion_verbose(
|
|
|
|
attr.span,
|
|
|
|
"perhaps you meant to use an outer attribute",
|
|
|
|
replacement,
|
|
|
|
rustc_errors::Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
2022-01-27 09:44:25 +00:00
|
|
|
err.emit();
|
2021-10-09 12:13:15 +00:00
|
|
|
}
|
2020-10-03 18:45:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 06:50:57 +00:00
|
|
|
fn check_non_exported_macro_for_invalid_attrs(tcx: TyCtxt<'_>, item: &Item<'_>) {
|
|
|
|
let attrs = tcx.hir().attrs(item.hir_id());
|
|
|
|
|
2021-02-01 14:35:53 +00:00
|
|
|
for attr in attrs {
|
2021-07-29 17:00:41 +00:00
|
|
|
if attr.has_name(sym::inline) {
|
2022-07-19 09:00:16 +00:00
|
|
|
tcx.sess.emit_err(errors::NonExportedMacroInvalidAttrs { attr_span: attr.span });
|
2021-02-01 14:35:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-27 11:09:54 +00:00
|
|
|
fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
2021-02-01 14:35:53 +00:00
|
|
|
let check_attr_visitor = &mut CheckAttrVisitor { tcx };
|
2022-07-03 13:28:57 +00:00
|
|
|
tcx.hir().visit_item_likes_in_module(module_def_id, check_attr_visitor);
|
2020-09-04 15:12:53 +00:00
|
|
|
if module_def_id.is_top_level_module() {
|
2022-01-05 12:02:16 +00:00
|
|
|
check_attr_visitor.check_attributes(CRATE_HIR_ID, DUMMY_SP, Target::Mod, None);
|
2020-10-03 18:45:39 +00:00
|
|
|
check_invalid_crate_level_attr(tcx, tcx.hir().krate_attrs());
|
2020-09-04 15:12:53 +00:00
|
|
|
}
|
2018-06-06 20:13:52 +00:00
|
|
|
}
|
|
|
|
|
2020-07-05 20:00:14 +00:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2018-06-06 20:13:52 +00:00
|
|
|
*providers = Providers { check_mod_attrs, ..*providers };
|
|
|
|
}
|
2021-09-05 23:30:37 +00:00
|
|
|
|
|
|
|
fn check_duplicates(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
attr: &Attribute,
|
|
|
|
hir_id: HirId,
|
|
|
|
duplicates: AttributeDuplicates,
|
|
|
|
seen: &mut FxHashMap<Symbol, Span>,
|
|
|
|
) {
|
|
|
|
use AttributeDuplicates::*;
|
|
|
|
if matches!(duplicates, WarnFollowingWordOnly) && !attr.is_word() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match duplicates {
|
|
|
|
DuplicatesOk => {}
|
|
|
|
WarnFollowing | FutureWarnFollowing | WarnFollowingWordOnly | FutureWarnPreceding => {
|
|
|
|
match seen.entry(attr.name_or_empty()) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
let (this, other) = if matches!(duplicates, FutureWarnPreceding) {
|
|
|
|
let to_remove = entry.insert(attr.span);
|
|
|
|
(to_remove, attr.span)
|
|
|
|
} else {
|
|
|
|
(attr.span, *entry.get())
|
|
|
|
};
|
2022-07-19 09:00:16 +00:00
|
|
|
tcx.emit_spanned_lint(
|
|
|
|
UNUSED_ATTRIBUTES,
|
|
|
|
hir_id,
|
|
|
|
this,
|
|
|
|
errors::UnusedDuplicate {
|
2021-09-05 23:30:37 +00:00
|
|
|
this,
|
2022-07-19 09:00:16 +00:00
|
|
|
other,
|
|
|
|
warning: matches!(
|
|
|
|
duplicates,
|
|
|
|
FutureWarnFollowing | FutureWarnPreceding
|
|
|
|
)
|
|
|
|
.then_some(()),
|
|
|
|
},
|
|
|
|
);
|
2021-09-05 23:30:37 +00:00
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(attr.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ErrorFollowing | ErrorPreceding => match seen.entry(attr.name_or_empty()) {
|
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
let (this, other) = if matches!(duplicates, ErrorPreceding) {
|
|
|
|
let to_remove = entry.insert(attr.span);
|
|
|
|
(to_remove, attr.span)
|
|
|
|
} else {
|
|
|
|
(attr.span, *entry.get())
|
|
|
|
};
|
2022-07-19 09:00:16 +00:00
|
|
|
tcx.sess.emit_err(errors::UnusedMultiple {
|
|
|
|
this,
|
|
|
|
other,
|
|
|
|
name: attr.name_or_empty(),
|
|
|
|
});
|
2021-09-05 23:30:37 +00:00
|
|
|
}
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
entry.insert(attr.span);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|