From 2728af7bc02ab48bf4dd861cb69b5b786ecb261d Mon Sep 17 00:00:00 2001 From: Mahdi Dibaiee Date: Tue, 11 Jan 2022 21:28:04 +0000 Subject: [PATCH] rustc_pass_by_value: handle inferred generic types (with _) --- compiler/rustc_lint/src/pass_by_value.rs | 14 +++++--------- .../internal-lints/rustc_pass_by_value.rs | 8 +++++--- .../internal-lints/rustc_pass_by_value.stderr | 18 +++++++++++++++--- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_lint/src/pass_by_value.rs b/compiler/rustc_lint/src/pass_by_value.rs index 3435a5a6c82d..26d0560bf89b 100644 --- a/compiler/rustc_lint/src/pass_by_value.rs +++ b/compiler/rustc_lint/src/pass_by_value.rs @@ -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::>(); diff --git a/src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.rs b/src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.rs index f8ab0f056d79..402c41f37660 100644 --- a/src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.rs +++ b/src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.rs @@ -105,11 +105,13 @@ struct WithParameters { } impl WithParameters { - fn test( + fn test<'a>( value: WithParameters, - reference: &WithParameters, //~ ERROR passing `WithParameters` by reference + reference: &'a WithParameters, //~ ERROR passing `WithParameters` by reference reference_with_m: &WithParameters, //~ ERROR passing `WithParameters` by reference - ) { + ) -> &'a WithParameters { + //~^ ERROR passing `WithParameters` by reference + reference as &WithParameters<_, 1> //~ ERROR passing `WithParameters<_, 1>` by reference } } diff --git a/src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.stderr b/src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.stderr index c5307f0f67d9..7f6e57b38f38 100644 --- a/src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.stderr +++ b/src/test/ui-fulldeps/internal-lints/rustc_pass_by_value.stderr @@ -103,8 +103,8 @@ LL | reference: &CustomAlias, error: passing `WithParameters` by reference --> $DIR/rustc_pass_by_value.rs:110:20 | -LL | reference: &WithParameters, - | ^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters` +LL | reference: &'a WithParameters, + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters` error: passing `WithParameters` by reference --> $DIR/rustc_pass_by_value.rs:111:27 @@ -112,5 +112,17 @@ error: passing `WithParameters` by reference LL | reference_with_m: &WithParameters, | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters` -error: aborting due to 18 previous errors +error: passing `WithParameters` by reference + --> $DIR/rustc_pass_by_value.rs:112:10 + | +LL | ) -> &'a WithParameters { + | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try passing by value: `WithParameters` + +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