Skip to content

Commit

Permalink
rustc_pass_by_value: handle inferred generic types (with _)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdibaiee committed Jan 11, 2022
1 parent 959bf2b commit 2728af7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
14 changes: 5 additions & 9 deletions compiler/rustc_lint/src/pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,15 @@ fn gen_args(cx: &LateContext<'_>, segment: &PathSegment<'_>) -> String {
let params = args
.args
.iter()
.filter_map(|arg| match arg {
GenericArg::Lifetime(lt) => Some(lt.name.ident().to_string()),
.map(|arg| match arg {
GenericArg::Lifetime(lt) => lt.name.ident().to_string(),
GenericArg::Type(ty) => {
let snippet =
cx.tcx.sess.source_map().span_to_snippet(ty.span).unwrap_or_default();
Some(snippet)
cx.tcx.sess.source_map().span_to_snippet(ty.span).unwrap_or_default()
}
GenericArg::Const(c) => {
let snippet =
cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_default();
Some(snippet)
cx.tcx.sess.source_map().span_to_snippet(c.span).unwrap_or_default()
}
_ => None,
GenericArg::Infer(_) => String::from("_"),
})
.collect::<Vec<_>>();

Expand Down
8 changes: 5 additions & 3 deletions src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,13 @@ struct WithParameters<T, const N: usize, M = u32> {
}

impl<T> WithParameters<T, 1> {
fn test(
fn test<'a>(
value: WithParameters<T, 1>,
reference: &WithParameters<T, 1>, //~ ERROR passing `WithParameters<T, 1>` by reference
reference: &'a WithParameters<T, 1>, //~ ERROR passing `WithParameters<T, 1>` by reference
reference_with_m: &WithParameters<T, 1, u32>, //~ ERROR passing `WithParameters<T, 1, u32>` by reference
) {
) -> &'a WithParameters<T, 1> {
//~^ ERROR passing `WithParameters<T, 1>` by reference
reference as &WithParameters<_, 1> //~ ERROR passing `WithParameters<_, 1>` by reference
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,26 @@ LL | reference: &CustomAlias,
error: passing `WithParameters<T, 1>` by reference
--> $DIR/rustc_pass_by_value.rs:110:20
|
LL | reference: &WithParameters<T, 1>,
| ^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters<T, 1>`
LL | reference: &'a WithParameters<T, 1>,
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters<T, 1>`

error: passing `WithParameters<T, 1, u32>` by reference
--> $DIR/rustc_pass_by_value.rs:111:27
|
LL | reference_with_m: &WithParameters<T, 1, u32>,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters<T, 1, u32>`

error: aborting due to 18 previous errors
error: passing `WithParameters<T, 1>` by reference
--> $DIR/rustc_pass_by_value.rs:112:10
|
LL | ) -> &'a WithParameters<T, 1> {
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters<T, 1>`

error: passing `WithParameters<_, 1>` by reference
--> $DIR/rustc_pass_by_value.rs:114:22
|
LL | reference as &WithParameters<_, 1>
| ^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters<_, 1>`

error: aborting due to 20 previous errors

0 comments on commit 2728af7

Please sign in to comment.