Skip to content

Commit

Permalink
rustc_pass_by_value remove dependency on rustc_diagnostic_item
Browse files Browse the repository at this point in the history
  • Loading branch information
mdibaiee committed Jan 10, 2022
1 parent 91ed689 commit 71e3314
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 18 deletions.
11 changes: 4 additions & 7 deletions compiler/rustc_lint/src/pass_by_value.rs
Expand Up @@ -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.
/// 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`)
/// This lint relies on `#[rustc_diagnostic_item]` being available for the target.
pub rustc::PASS_BY_VALUE,
Warn,
"pass by reference of a type flagged as `#[rustc_pass_by_value]`",
Expand Down Expand Up @@ -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 {
match path.res {
Res::Def(_, def_id) if has_pass_by_value_attr(cx, def_id) => {
if let Some(name) = cx.tcx.get_diagnostic_name(def_id) {
return Some(format!("{}{}", name, gen_args(path.segments.last().unwrap())));
}
let name = cx.tcx.item_name(def_id).to_ident_string();
return Some(format!("{}{}", name, gen_args(path.segments.last().unwrap())));
}
Res::SelfTy(None, Some((did, _))) => {
if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() {
if has_pass_by_value_attr(cx, adt.did) {
if let Some(name) = cx.tcx.get_diagnostic_name(adt.did) {
return Some(format!("{}<{}>", name, substs[0]));
}
let name = cx.tcx.item_name(adt.did).to_ident_string();
return Some(format!("{}<{}>", name, substs[0]));
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.rs
Expand Up @@ -62,7 +62,6 @@ impl Foo {
//~^^ ERROR passing `TyCtxt<'_>` by reference
}

#[rustc_diagnostic_item = "CustomEnum"]
#[rustc_pass_by_value]
enum CustomEnum {
A,
Expand All @@ -77,13 +76,11 @@ impl CustomEnum {
}
}

#[rustc_diagnostic_item = "CustomStruct"]
#[rustc_pass_by_value]
struct CustomStruct {
s: u8,
}

#[rustc_diagnostic_item = "CustomAlias"]
#[rustc_pass_by_value]
type CustomAlias<'a> = &'a CustomStruct; //~ ERROR passing `CustomStruct` by reference

Expand Down
Expand Up @@ -77,25 +77,25 @@ LL | fn ty_multi_ref_assoc(ty_multi: &&Ty<'_>, ty_ctxt_multi: &&&&TyCtxt<'_>
| ^^^^^^^^^^^ help: try passing by value: `TyCtxt<'_>`

error: passing `CustomEnum` by reference
--> $DIR/rustc_pass_by_value.rs:75:20
--> $DIR/rustc_pass_by_value.rs:74:20
|
LL | reference: &CustomEnum,
| ^^^^^^^^^^^ help: try passing by value: `CustomEnum`

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;
| ^^^^^^^^^^^^^^^^ help: try passing by value: `CustomStruct`

error: passing `CustomStruct` by reference
--> $DIR/rustc_pass_by_value.rs:93:20
--> $DIR/rustc_pass_by_value.rs:90:20
|
LL | reference: &CustomStruct,
| ^^^^^^^^^^^^^ help: try passing by value: `CustomStruct`

error: passing `CustomAlias<>` by reference
--> $DIR/rustc_pass_by_value.rs:99:20
--> $DIR/rustc_pass_by_value.rs:96:20
|
LL | reference: &CustomAlias,
| ^^^^^^^^^^^^ help: try passing by value: `CustomAlias<>`
Expand Down
Expand Up @@ -8,7 +8,6 @@
#![deny(rustc::pass_by_value)]
#![allow(unused)]

#[rustc_diagnostic_item = "TyCtxt"]
#[rustc_pass_by_value]
struct TyCtxt<'tcx> {
inner: &'tcx (),
Expand All @@ -23,7 +22,6 @@ struct TyS<'tcx> {
inner: &'tcx (),
}

#[rustc_diagnostic_item = "Ty"]
#[rustc_pass_by_value]
type Ty<'tcx> = &'tcx TyS<'tcx>;

Expand Down
@@ -1,5 +1,5 @@
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) {}
| ^^^^^ help: try passing by value: `TyCtxt<'tcx>`
Expand All @@ -11,7 +11,7 @@ LL | #![deny(rustc::pass_by_value)]
| ^^^^^^^^^^^^^^^^^^^^

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>) {}
| ^^^^^^^^^ help: try passing by value: `Ty<'tcx>`
Expand Down

0 comments on commit 71e3314

Please sign in to comment.