Skip to content

Commit 2c44398

Browse files
committed
Adress review comments
1 parent 10b7fab commit 2c44398

8 files changed

+258
-118
lines changed

clippy_lints/src/lifetimes.rs

+31-50
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ fn explicit_self_type<'tcx>(cx: &LateContext<'tcx>, func: &FnDecl<'tcx>, ident:
236236
}
237237
}
238238

239-
#[expect(clippy::too_many_lines)]
240239
fn could_use_elision<'tcx>(
241240
cx: &LateContext<'tcx>,
242241
func: &'tcx FnDecl<'_>,
@@ -331,50 +330,32 @@ fn could_use_elision<'tcx>(
331330
}
332331
}
333332

334-
// no input lifetimes? easy case!
335-
if input_lts.is_empty() {
336-
None
337-
} else if output_lts.is_empty() {
338-
// no output lifetimes, check distinctness of input lifetimes
333+
// A lifetime can be newly elided if:
334+
// - It occurs only once among the inputs.
335+
// - If there are multiple input lifetimes, then the newly elided lifetime does not occur among the
336+
// outputs (because eliding such an lifetime would create an ambiguity).
337+
let elidable_lts = named_lifetime_occurrences(&input_lts)
338+
.into_iter()
339+
.filter_map(|(def_id, occurrences)| {
340+
if occurrences <= 1 && (input_lts.len() <= 1 || !output_lts.contains(&RefLt::Named(def_id))) {
341+
Some((
342+
def_id,
343+
input_visitor
344+
.lifetime_generic_arg_spans
345+
.get(&def_id)
346+
.or_else(|| output_visitor.lifetime_generic_arg_spans.get(&def_id))
347+
.copied(),
348+
))
349+
} else {
350+
None
351+
}
352+
})
353+
.collect::<Vec<_>>();
339354

340-
// only unnamed and static, ok
341-
let unnamed_and_static = input_lts.iter().all(|lt| *lt == RefLt::Unnamed || *lt == RefLt::Static);
342-
if unnamed_and_static {
343-
return None;
344-
}
345-
// we have no output reference, so we can elide explicit lifetimes that occur at most once
346-
let elidable_lts = named_lifetime_occurrences(&input_lts)
347-
.into_iter()
348-
.filter_map(|(def_id, occurrences)| {
349-
if occurrences <= 1 {
350-
Some((def_id, input_visitor.sample_generic_arg_span.get(&def_id).copied()))
351-
} else {
352-
None
353-
}
354-
})
355-
.collect::<Vec<_>>();
356-
if elidable_lts.is_empty() {
357-
None
358-
} else {
359-
Some(elidable_lts)
360-
}
355+
if elidable_lts.is_empty() {
356+
None
361357
} else {
362-
// we have output references, so we need one input reference,
363-
// and all output lifetimes must be the same
364-
if input_lts.len() == 1 {
365-
match (&input_lts[0], &output_lts[0]) {
366-
(&RefLt::Named(n1), &RefLt::Named(n2)) if n1 == n2 => {
367-
Some(vec![(n1, input_visitor.sample_generic_arg_span.get(&n1).copied())])
368-
},
369-
(&RefLt::Named(n), &RefLt::Unnamed) => {
370-
Some(vec![(n, input_visitor.sample_generic_arg_span.get(&n).copied())])
371-
},
372-
_ => None, /* already elided, different named lifetimes
373-
* or something static going on */
374-
}
375-
} else {
376-
None
377-
}
358+
Some(elidable_lts)
378359
}
379360
}
380361

@@ -397,11 +378,11 @@ fn named_lifetime_occurrences(lts: &[RefLt]) -> Vec<(LocalDefId, usize)> {
397378
let mut occurrences = Vec::new();
398379
for lt in lts {
399380
if let &RefLt::Named(curr_def_id) = lt {
400-
if let Some(i) = occurrences
401-
.iter()
402-
.position(|&(prev_def_id, _)| prev_def_id == curr_def_id)
381+
if let Some(pair) = occurrences
382+
.iter_mut()
383+
.find(|(prev_def_id, _)| *prev_def_id == curr_def_id)
403384
{
404-
occurrences[i].1 += 1;
385+
pair.1 += 1;
405386
} else {
406387
occurrences.push((curr_def_id, 1));
407388
}
@@ -416,7 +397,7 @@ const CLOSURE_TRAIT_BOUNDS: [LangItem; 3] = [LangItem::Fn, LangItem::FnMut, Lang
416397
struct RefVisitor<'a, 'tcx> {
417398
cx: &'a LateContext<'tcx>,
418399
lts: Vec<RefLt>,
419-
sample_generic_arg_span: FxHashMap<LocalDefId, Span>,
400+
lifetime_generic_arg_spans: FxHashMap<LocalDefId, Span>,
420401
nested_elision_site_lts: Vec<RefLt>,
421402
unelided_trait_object_lifetime: bool,
422403
}
@@ -426,7 +407,7 @@ impl<'a, 'tcx> RefVisitor<'a, 'tcx> {
426407
Self {
427408
cx,
428409
lts: Vec::new(),
429-
sample_generic_arg_span: FxHashMap::default(),
410+
lifetime_generic_arg_spans: FxHashMap::default(),
430411
nested_elision_site_lts: Vec::new(),
431412
unelided_trait_object_lifetime: false,
432413
}
@@ -525,7 +506,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RefVisitor<'a, 'tcx> {
525506
if let GenericArg::Lifetime(l) = generic_arg
526507
&& let LifetimeName::Param(def_id, _) = l.name
527508
{
528-
self.sample_generic_arg_span.entry(def_id).or_insert(l.span);
509+
self.lifetime_generic_arg_spans.entry(def_id).or_insert(l.span);
529510
}
530511
// Replace with `walk_generic_arg` if/when https://github.com/rust-lang/rust/pull/103692 lands.
531512
// walk_generic_arg(self, generic_arg);

clippy_lints/src/matches/significant_drop_in_scrutinee.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ fn set_diagnostic<'tcx>(diag: &mut Diagnostic, cx: &LateContext<'tcx>, expr: &'t
8383

8484
/// If the expression is an `ExprKind::Match`, check if the scrutinee has a significant drop that
8585
/// may have a surprising lifetime.
86-
fn has_significant_drop_in_scrutinee<'tcx, 'a>(
87-
cx: &'a LateContext<'tcx>,
86+
fn has_significant_drop_in_scrutinee<'tcx>(
87+
cx: &LateContext<'tcx>,
8888
scrutinee: &'tcx Expr<'tcx>,
8989
source: MatchSource,
9090
) -> Option<(Vec<FoundSigDrop>, &'static str)> {

tests/ui/mut_from_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![allow(unused)]
1+
#![allow(unused, clippy::needless_lifetimes)]
22
#![warn(clippy::mut_from_ref)]
33

44
struct Foo;

tests/ui/needless_lifetimes.rs

+79-11
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,20 @@ fn multiple_in_and_out_1<'a>(x: &'a u8, _y: &'a u8) -> &'a u8 {
2929
x
3030
}
3131

32-
// No error; multiple input refs.
33-
fn multiple_in_and_out_2<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
32+
// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
33+
// fn multiple_in_and_out_2a<'a>(x: &'a u8, _y: &u8) -> &'a u8
34+
// ^^^
35+
fn multiple_in_and_out_2a<'a, 'b>(x: &'a u8, _y: &'b u8) -> &'a u8 {
3436
x
3537
}
3638

39+
// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
40+
// fn multiple_in_and_out_2b<'b>(_x: &u8, y: &'b u8) -> &'b u8
41+
// ^^^
42+
fn multiple_in_and_out_2b<'a, 'b>(_x: &'a u8, y: &'b u8) -> &'b u8 {
43+
y
44+
}
45+
3746
// No error; multiple input refs
3847
async fn func<'a>(args: &[&'a str]) -> Option<&'a str> {
3948
args.get(0).cloned()
@@ -44,11 +53,20 @@ fn in_static_and_out<'a>(x: &'a u8, _y: &'static u8) -> &'a u8 {
4453
x
4554
}
4655

47-
// No error.
48-
fn deep_reference_1<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
56+
// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
57+
// fn deep_reference_1a<'a>(x: &'a u8, _y: &u8) -> Result<&'a u8, ()>
58+
// ^^^
59+
fn deep_reference_1a<'a, 'b>(x: &'a u8, _y: &'b u8) -> Result<&'a u8, ()> {
4960
Ok(x)
5061
}
5162

63+
// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
64+
// fn deep_reference_1b<'b>(_x: &u8, y: &'b u8) -> Result<&'b u8, ()>
65+
// ^^^
66+
fn deep_reference_1b<'a, 'b>(_x: &'a u8, y: &'b u8) -> Result<&'b u8, ()> {
67+
Ok(y)
68+
}
69+
5270
// No error; two input refs.
5371
fn deep_reference_2<'a>(x: Result<&'a u8, &'a u8>) -> &'a u8 {
5472
x.unwrap()
@@ -129,11 +147,20 @@ impl X {
129147
&self.x
130148
}
131149

132-
// No error; multiple input refs.
133-
fn self_and_in_out<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
150+
// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
151+
// fn self_and_in_out_1<'s>(&'s self, _x: &u8) -> &'s u8
152+
// ^^^
153+
fn self_and_in_out_1<'s, 't>(&'s self, _x: &'t u8) -> &'s u8 {
134154
&self.x
135155
}
136156

157+
// Error; multiple input refs, but the output lifetime is not elided, i.e., the following is valid:
158+
// fn self_and_in_out_2<'t>(&self, x: &'t u8) -> &'t u8
159+
// ^^^^^
160+
fn self_and_in_out_2<'s, 't>(&'s self, x: &'t u8) -> &'t u8 {
161+
x
162+
}
163+
137164
fn distinct_self_and_in<'s, 't>(&'s self, _x: &'t u8) {}
138165

139166
// No error; same lifetimes on two params.
@@ -167,8 +194,19 @@ fn struct_with_lt3<'a>(_foo: &Foo<'a>) -> &'a str {
167194
unimplemented!()
168195
}
169196

170-
// No warning; two input lifetimes.
171-
fn struct_with_lt4<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
197+
// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
198+
// valid:
199+
// fn struct_with_lt4a<'a>(_foo: &'a Foo<'_>) -> &'a str
200+
// ^^
201+
fn struct_with_lt4a<'a, 'b>(_foo: &'a Foo<'b>) -> &'a str {
202+
unimplemented!()
203+
}
204+
205+
// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
206+
// valid:
207+
// fn struct_with_lt4b<'b>(_foo: &Foo<'b>) -> &'b str
208+
// ^^^^
209+
fn struct_with_lt4b<'a, 'b>(_foo: &'a Foo<'b>) -> &'b str {
172210
unimplemented!()
173211
}
174212

@@ -203,8 +241,19 @@ fn alias_with_lt3<'a>(_foo: &FooAlias<'a>) -> &'a str {
203241
unimplemented!()
204242
}
205243

206-
// No warning; two input lifetimes.
207-
fn alias_with_lt4<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
244+
// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
245+
// valid:
246+
// fn alias_with_lt4a<'a>(_foo: &'a FooAlias<'_>) -> &'a str
247+
// ^^
248+
fn alias_with_lt4a<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'a str {
249+
unimplemented!()
250+
}
251+
252+
// Warning; two input lifetimes, but the output lifetime is not elided, i.e., the following is
253+
// valid:
254+
// fn alias_with_lt4b<'b>(_foo: &FooAlias<'b>) -> &'b str
255+
// ^^^^^^^^^
256+
fn alias_with_lt4b<'a, 'b>(_foo: &'a FooAlias<'b>) -> &'b str {
208257
unimplemented!()
209258
}
210259

@@ -419,12 +468,31 @@ mod issue7296 {
419468
}
420469
}
421470

422-
mod false_negative {
471+
mod pr_9743_false_negative_fix {
423472
#![allow(unused)]
424473

425474
fn foo<'a>(x: &'a u8, y: &'_ u8) {}
426475

427476
fn bar<'a>(x: &'a u8, y: &'_ u8, z: &'_ u8) {}
428477
}
429478

479+
mod pr_9743_output_lifetime_checks {
480+
#![allow(unused)]
481+
482+
// lint: only one input
483+
fn one_input<'a>(x: &'a u8) -> &'a u8 {
484+
unimplemented!()
485+
}
486+
487+
// lint: multiple inputs, output would not be elided
488+
fn multiple_inputs_output_not_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'b u8 {
489+
unimplemented!()
490+
}
491+
492+
// don't lint: multiple inputs, output would be elided (which would create an ambiguity)
493+
fn multiple_inputs_output_would_be_elided<'a, 'b>(x: &'a u8, y: &'b u8, z: &'b u8) -> &'a u8 {
494+
unimplemented!()
495+
}
496+
}
497+
430498
fn main() {}

0 commit comments

Comments
 (0)