+// run-rustfix
+// aux-build:proc_macros.rs
+
#![warn(clippy::needless_lifetimes)]
#![allow(
- dead_code,
+ unused,
clippy::boxed_local,
+ clippy::extra_unused_type_parameters,
clippy::needless_pass_by_value,
clippy::unnecessary_wraps,
- dyn_drop
+ dyn_drop,
+ clippy::get_first
)]
+extern crate proc_macros;
+use proc_macros::inline_macros;
+
fn distinct_lifetimes<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: u8) {}
fn distinct_and_static<'a, 'b>(_x: &'a u8, _y: &'b u8, _z: &'static u8) {}
x
}
-// No error; multiple input refs.
-fn multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
+// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
+// fn multiple_in_and_out_2a<'a>(x: &'a u8, _y: &u8) -> &'a u8
+// ^^^
+fn multiple_in_and_out_2a<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
x
}
+// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
+// fn multiple_in_and_out_2b<'b>(_x: &u8, y: &'b u8) -> &'b u8
+// ^^^
+fn multiple_in_and_out_2b<'a, 'b>(_x: &'a u8, y: &'b u8) -> &'b u8 {
+ y
+}
+
// No error; multiple input refs
async fn func<'a>(args: &[&'a str]) -> Option<&'a str> {
args.get(0).cloned()
x
}
-// No error.
-fn deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
+// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
+// fn deep_reference_1a<'a>(x: &'a u8, _y: &u8) -> Result<&'a u8, ()>
+// ^^^
+fn deep_reference_1a<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
Ok(x)
}
+// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
+// fn deep_reference_1b<'b>(_x: &u8, y: &'b u8) -> Result<&'b u8, ()>
+// ^^^
+fn deep_reference_1b<'a, 'b>(_x: &'a u8, y: &'b u8) -> Result<&'b u8, ()> {
+ Ok(y)
+}
+
// No error; two input refs.
fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 {
x.unwrap()
&self.x
}
- // No error; multiple input refs.
- fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
+ // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
+ // fn self_and_in_out_1<'s>(&'s self, _x: &u8) -> &'s u8
+ // ^^^
+ fn self_and_in_out_1<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
&self.x
}
+ // Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
+ // fn self_and_in_out_2<'t>(&self, x: &'t u8) -> &'t u8
+ // ^^^^^
+ fn self_and_in_out_2<'s, 't>(&'s self, x: &'t u8) -> &'t u8 {
+ x
+ }
+
fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {}
// No error; same lifetimes on two params.
unimplemented!()
}
-// No warning; two input lifetimes.
-fn struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
+// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
+// valid:
+// fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str
+// ^^
+fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
+ unimplemented!()
+}
+
+// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
+// valid:
+// fn struct_with_lt4b<'b>(_foo: &Foo<'b>) -> &'b str
+// ^^^^
+fn struct_with_lt4b<'a, 'b>(_foo: &'a Foo<'b>) -> &'b str {
unimplemented!()
}
unimplemented!()
}
-// No warning; two input lifetimes.
-fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
+// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
+// valid:
+// fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str
+// ^^
+fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
+ unimplemented!()
+}
+
+// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
+// valid:
+// fn alias_with_lt4b<'b>(_foo: &FooAlias<'b>) -> &'b str
+// ^^^^^^^^^
+fn alias_with_lt4b<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'b str {
unimplemented!()
}
}
}
+mod pr_9743_false_negative_fix {
+ #![allow(unused)]
+
+ fn foo<'a>(x: &'a u8, y: &'_ u8) {}
+
+ fn bar<'a>(x: &'a u8, y: &'_ u8, z: &'_ u8) {}
+}
+
+mod pr_9743_output_lifetime_checks {
+ #![allow(unused)]
+
+ // lint: only one input
+ fn one_input<'a>(x: &'a u8) -> &'a u8 {
+ unimplemented!()
+ }
+
+ // lint: multiple inputs, output would not be elided
+ fn multiple_inputs_output_not_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'b u8 {
+ unimplemented!()
+ }
+
+ // don't lint: multiple inputs, output would be elided (which would create an ambiguity)
+ fn multiple_inputs_output_would_be_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'a u8 {
+ unimplemented!()
+ }
+}
+
+#[inline_macros]
+mod in_macro {
+ use proc_macros::external;
+
+ // lint local macro expands to function with needless lifetimes
+ inline! {
+ fn one_input<'a>(x: &'a u8) -> &'a u8 {
+ unimplemented!()
+ }
+ }
+
+ // no lint on external macro
+ external! {
+ fn needless_lifetime<'a>(x: &'a u8) -> &'a u8 {
+ unimplemented!()
+ }
+ }
+
+ inline! {
+ fn f<$'a>(arg: &$'a str) -> &$'a str {
+ arg
+ }
+ }
+}
+
+mod issue5787 {
+ use std::sync::MutexGuard;
+
+ struct Foo;
+
+ impl Foo {
+ // doesn't get linted without async
+ pub async fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> {
+ guard
+ }
+ }
+
+ async fn foo<'a>(_x: &i32, y: &'a str) -> &'a str {
+ y
+ }
+}
+
fn main() {}