rustc_pass_by_value remove dependency on rustc_diagnostic_item

This commit is contained in:
Mahdi Dibaiee 2022-01-10 18:12:28 +00:00
parent 91ed6892f7
commit 71e3314673
No known key found for this signature in database
GPG key ID: BABA115BDF0C598A
5 changed files with 10 additions and 18 deletions

View file

@ -11,7 +11,6 @@ declare_tool_lint! {
/// The `rustc_pass_by_value` lint marks a type with `#[rustc_pass_by_value]` requiring it to always be passed by value. /// The `rustc_pass_by_value` lint marks a type with `#[rustc_pass_by_value]` requiring it to always be passed by value.
/// This is usually used for types that are thin wrappers around references, so there is no benefit to an extra /// This is usually used for types that are thin wrappers around references, so there is no benefit to an extra
/// layer of indirection. (Example: `Ty` which is a reference to a `TyS`) /// layer of indirection. (Example: `Ty` which is a reference to a `TyS`)
/// This lint relies on `#[rustc_diagnostic_item]` being available for the target.
pub rustc::PASS_BY_VALUE, pub rustc::PASS_BY_VALUE,
Warn, Warn,
"pass by reference of a type flagged as `#[rustc_pass_by_value]`", "pass by reference of a type flagged as `#[rustc_pass_by_value]`",
@ -52,16 +51,14 @@ fn path_for_pass_by_value(cx: &LateContext<'_>, ty: &hir::Ty<'_>) -> Option<Stri
if let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind { if let TyKind::Path(QPath::Resolved(_, path)) = &ty.kind {
match path.res { match path.res {
Res::Def(_, def_id) if has_pass_by_value_attr(cx, def_id) => { Res::Def(_, def_id) if has_pass_by_value_attr(cx, def_id) => {
if let Some(name) = cx.tcx.get_diagnostic_name(def_id) { let name = cx.tcx.item_name(def_id).to_ident_string();
return Some(format!("{}{}", name, gen_args(path.segments.last().unwrap()))); return Some(format!("{}{}", name, gen_args(path.segments.last().unwrap())));
}
} }
Res::SelfTy(None, Some((did, _))) => { Res::SelfTy(None, Some((did, _))) => {
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() { if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
if has_pass_by_value_attr(cx, adt.did) { if has_pass_by_value_attr(cx, adt.did) {
if let Some(name) = cx.tcx.get_diagnostic_name(adt.did) { let name = cx.tcx.item_name(adt.did).to_ident_string();
return Some(format!("{}<{}>", name, substs[0])); return Some(format!("{}<{}>", name, substs[0]));
}
} }
} }
} }

View file

@ -62,7 +62,6 @@ impl Foo {
//~^^ ERROR passing `TyCtxt<'_>` by reference //~^^ ERROR passing `TyCtxt<'_>` by reference
} }
#[rustc_diagnostic_item = "CustomEnum"]
#[rustc_pass_by_value] #[rustc_pass_by_value]
enum CustomEnum { enum CustomEnum {
A, A,
@ -77,13 +76,11 @@ impl CustomEnum {
} }
} }
#[rustc_diagnostic_item = "CustomStruct"]
#[rustc_pass_by_value] #[rustc_pass_by_value]
struct CustomStruct { struct CustomStruct {
s: u8, s: u8,
} }
#[rustc_diagnostic_item = "CustomAlias"]
#[rustc_pass_by_value] #[rustc_pass_by_value]
type CustomAlias<'a> = &'a CustomStruct; //~ ERROR passing `CustomStruct` by reference type CustomAlias<'a> = &'a CustomStruct; //~ ERROR passing `CustomStruct` by reference

View file

@ -77,25 +77,25 @@ LL | fn ty_multi_ref_assoc(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>
| ^^^^^^^^^^^ help: try passing by value: `TyCtxt<'_>` | ^^^^^^^^^^^ help: try passing by value: `TyCtxt<'_>`
error: passing `CustomEnum` by reference error: passing `CustomEnum` by reference
--> $DIR/rustc_pass_by_value.rs:75:20 --> $DIR/rustc_pass_by_value.rs:74:20
| |
LL | reference: &CustomEnum, LL | reference: &CustomEnum,
| ^^^^^^^^^^^ help: try passing by value: `CustomEnum` | ^^^^^^^^^^^ help: try passing by value: `CustomEnum`
error: passing `CustomStruct` by reference error: passing `CustomStruct` by reference
--> $DIR/rustc_pass_by_value.rs:88:24 --> $DIR/rustc_pass_by_value.rs:85:24
| |
LL | type CustomAlias<'a> = &'a CustomStruct; LL | type CustomAlias<'a> = &'a CustomStruct;
| ^^^^^^^^^^^^^^^^ help: try passing by value: `CustomStruct` | ^^^^^^^^^^^^^^^^ help: try passing by value: `CustomStruct`
error: passing `CustomStruct` by reference error: passing `CustomStruct` by reference
--> $DIR/rustc_pass_by_value.rs:93:20 --> $DIR/rustc_pass_by_value.rs:90:20
| |
LL | reference: &CustomStruct, LL | reference: &CustomStruct,
| ^^^^^^^^^^^^^ help: try passing by value: `CustomStruct` | ^^^^^^^^^^^^^ help: try passing by value: `CustomStruct`
error: passing `CustomAlias<>` by reference error: passing `CustomAlias<>` by reference
--> $DIR/rustc_pass_by_value.rs:99:20 --> $DIR/rustc_pass_by_value.rs:96:20
| |
LL | reference: &CustomAlias, LL | reference: &CustomAlias,
| ^^^^^^^^^^^^ help: try passing by value: `CustomAlias<>` | ^^^^^^^^^^^^ help: try passing by value: `CustomAlias<>`

View file

@ -8,7 +8,6 @@
#![deny(rustc::pass_by_value)] #![deny(rustc::pass_by_value)]
#![allow(unused)] #![allow(unused)]
#[rustc_diagnostic_item = "TyCtxt"]
#[rustc_pass_by_value] #[rustc_pass_by_value]
struct TyCtxt<'tcx> { struct TyCtxt<'tcx> {
inner: &'tcx (), inner: &'tcx (),
@ -23,7 +22,6 @@ struct TyS<'tcx> {
inner: &'tcx (), inner: &'tcx (),
} }
#[rustc_diagnostic_item = "Ty"]
#[rustc_pass_by_value] #[rustc_pass_by_value]
type Ty<'tcx> = &'tcx TyS<'tcx>; type Ty<'tcx> = &'tcx TyS<'tcx>;

View file

@ -1,5 +1,5 @@
error: passing `TyCtxt<'tcx>` by reference error: passing `TyCtxt<'tcx>` by reference
--> $DIR/rustc_pass_by_value_self.rs:19:15 --> $DIR/rustc_pass_by_value_self.rs:18:15
| |
LL | fn by_ref(&self) {} LL | fn by_ref(&self) {}
| ^^^^^ help: try passing by value: `TyCtxt<'tcx>` | ^^^^^ help: try passing by value: `TyCtxt<'tcx>`
@ -11,7 +11,7 @@ LL | #![deny(rustc::pass_by_value)]
| ^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^
error: passing `Ty<'tcx>` by reference error: passing `Ty<'tcx>` by reference
--> $DIR/rustc_pass_by_value_self.rs:32:21 --> $DIR/rustc_pass_by_value_self.rs:30:21
| |
LL | fn by_ref(self: &Ty<'tcx>) {} LL | fn by_ref(self: &Ty<'tcx>) {}
| ^^^^^^^^^ help: try passing by value: `Ty<'tcx>` | ^^^^^^^^^ help: try passing by value: `Ty<'tcx>`