mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
Add const-anon suggestion for non local impl
This commit is contained in:
parent
a8ae1175c7
commit
85e3a2ee04
@ -421,6 +421,7 @@ lint_non_local_definitions_impl = non-local `impl` definition, they should be av
|
||||
}
|
||||
.non_local = an `impl` definition is non-local if it is nested inside an item and neither the type nor the trait are at the same nesting level as the `impl` block
|
||||
.exception = one exception to the rule are anon-const (`const _: () = {"{"} ... {"}"}`) at top-level module and anon-const at the same nesting as the trait or type
|
||||
.const_anon = use a const-anon item to suppress this lint
|
||||
|
||||
lint_non_local_definitions_macro_rules = non-local `macro_rules!` definition, they should be avoided as they go against expectation
|
||||
.help =
|
||||
|
@ -1301,7 +1301,13 @@ pub enum NonLocalDefinitionsDiag {
|
||||
#[note(lint_non_local)]
|
||||
#[note(lint_exception)]
|
||||
#[note(lint_non_local_definitions_deprecation)]
|
||||
Impl { depth: u32, body_kind_descr: &'static str, body_name: String },
|
||||
Impl {
|
||||
depth: u32,
|
||||
body_kind_descr: &'static str,
|
||||
body_name: String,
|
||||
#[suggestion(lint_const_anon, code = "_", applicability = "machine-applicable")]
|
||||
const_anon: Option<Span>,
|
||||
},
|
||||
#[diag(lint_non_local_definitions_macro_rules)]
|
||||
#[help]
|
||||
#[note(lint_non_local)]
|
||||
|
@ -1,4 +1,4 @@
|
||||
use rustc_hir::{def::DefKind, Body, Item, ItemKind, Path, QPath, TyKind};
|
||||
use rustc_hir::{def::DefKind, Body, Item, ItemKind, Node, Path, QPath, TyKind};
|
||||
use rustc_span::{def_id::DefId, sym, symbol::kw, MacroKind};
|
||||
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
@ -140,6 +140,19 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
|
||||
// If none of them have a local parent (LOGICAL NOR) this means that
|
||||
// this impl definition is a non-local definition and so we lint on it.
|
||||
if !(self_ty_has_local_parent || of_trait_has_local_parent) {
|
||||
let const_anon = if self.body_depth == 1
|
||||
&& parent_def_kind == DefKind::Const
|
||||
&& parent_opt_item_name != Some(kw::Underscore)
|
||||
&& let Some(parent) = parent.as_local()
|
||||
&& let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent)
|
||||
&& let ItemKind::Const(ty, _, _) = item.kind
|
||||
&& let TyKind::Tup(&[]) = ty.kind
|
||||
{
|
||||
Some(item.ident.span)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
cx.emit_span_lint(
|
||||
NON_LOCAL_DEFINITIONS,
|
||||
item.span,
|
||||
@ -149,6 +162,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
|
||||
body_name: parent_opt_item_name
|
||||
.map(|s| s.to_ident_string())
|
||||
.unwrap_or_else(|| "<unnameable>".to_string()),
|
||||
const_anon,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
warning: non-local `impl` definition, they should be avoided as they go against expectation
|
||||
--> $DIR/non_local_definitions.rs:32:5
|
||||
|
|
||||
LL | const Z: () = {
|
||||
| - help: use a const-anon item to suppress this lint: `_`
|
||||
...
|
||||
LL | impl Uto for &Test {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
|
Loading…
Reference in New Issue
Block a user