2 #![feature(custom_inner_attributes, lint_reasons)]
4 #[warn(clippy::all, clippy::needless_borrow)]
5 #[allow(unused_variables)]
7 clippy::uninlined_format_args,
8 clippy::unnecessary_mut_passed,
9 clippy::unnecessary_to_owned
14 let _ = x(&a); // no warning
15 let _ = x(&&a); // warn
18 mut_ref(&mut b); // no warning
19 mut_ref(&mut &mut b); // warn
21 let s = &String::from("hi");
22 let s_ident = f(&s); // should not error, because `&String` implements Copy, but `String` does not
23 let g_val = g(&Vec::new()); // should not error, because `&Vec<T>` derefs to `&[T]`
25 let vec_val = g(&vec); // should not error, because `&Vec<T>` derefs to `&[T]`
26 h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait`
27 let garbl = match 42 {
61 let y: &mut i32 = &mut x;
62 let y: &mut i32 = &mut &mut x;
65 // Don't lint. Removing the borrow would move 'x'
69 let y: &mut i32 = match 0 {
70 // Lint here. The type given above triggers auto-borrow.
74 fn ref_mut_i32(_: &mut i32) {}
76 // Lint here. The type given above triggers auto-borrow.
80 // use 'x' after to make sure it's still usable in the fixed code.
83 let s = String::new();
84 // let _ = (&s).len();
85 // let _ = (&s).capacity();
86 // let _ = (&&s).capacity();
90 let x = &x as *const (i32, i32);
91 let _ = unsafe { (&*x).0 };
100 (&()).foo(); // Don't lint. `()` doesn't implement `Foo`
106 impl Foo for &'_ i32 {
109 (&5).foo(); // Don't lint. `5` will call `<i32 as Foo>::foo`
118 impl FooRef for &'_ () {
121 (&&()).foo_ref(); // Don't lint. `&()` will call `<() as FooRef>::foo_ref`
124 impl From<S> for u32 {
125 fn from(s: S) -> Self {
129 impl From<&S> for u32 {
130 fn from(s: &S) -> Self {
135 let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
136 let _ = std::path::Path::new(".").join(&&".");
137 deref_target_is_x(&X);
138 multiple_constraints(&[[""]]);
139 multiple_constraints_normalizes_to_same(&X, X);
140 let _ = Some("").unwrap_or(&"");
141 let _ = std::fs::write("x", &"".to_string());
143 only_sized(&""); // Don't lint. `Sized` is only bound
144 let _ = std::any::Any::type_id(&""); // Don't lint. `Any` is only bound
145 let _ = Box::new(&""); // Don't lint. Type parameter appears in return type
146 ref_as_ref_path(&""); // Don't lint. Argument type is not a type parameter
147 refs_only(&()); // Don't lint. `&T` implements trait, but `T` doesn't
148 multiple_constraints_normalizes_to_different(&[[""]], &[""]); // Don't lint. Projected type appears in arguments
151 #[allow(clippy::needless_borrowed_reference)]
152 fn x(y: &i32) -> i32 {
156 fn mut_ref(y: &mut i32) {
160 fn f<T: Copy>(y: &T) -> T {
164 fn g(y: &[u8]) -> u8 {
170 impl<'a> Trait for &'a str {}
172 fn h(_: &dyn Trait) {}
175 fn check_expect_suppression() {
177 #[expect(clippy::needless_borrow)]
191 fn calls_field(&self) -> T {
200 fn calls_mut_field(&mut self) -> T {
206 #[derive(Clone, Copy)]
209 impl std::ops::Deref for X {
211 fn deref(&self) -> &Self::Target {
216 fn deref_target_is_x<T>(_: T)
218 T: std::ops::Deref<Target = X>,
222 fn multiple_constraints<T, U, V, X, Y>(_: T)
224 T: IntoIterator<Item = U> + IntoIterator<Item = X>,
225 U: IntoIterator<Item = V>,
227 X: IntoIterator<Item = Y>,
228 Y: AsRef<std::ffi::OsStr>,
232 fn multiple_constraints_normalizes_to_same<T, U, V>(_: T, _: V)
234 T: std::ops::Deref<Target = U>,
235 U: std::ops::Deref<Target = V>,
239 fn only_sized<T>(_: T) {}
241 fn ref_as_ref_path<T: 'static>(_: &'static T)
243 &'static T: AsRef<std::path::Path>,
251 impl<T> RefsOnly for &T {
255 fn refs_only<T, U>(_: T)
257 T: RefsOnly<Referent = U>,
261 fn multiple_constraints_normalizes_to_different<T, U, V>(_: T, _: U)
263 T: IntoIterator<Item = U>,
264 U: IntoIterator<Item = V>,
269 // https://github.com/rust-lang/rust-clippy/pull/9136#pullrequestreview-1037379321
271 mod copyable_iterator {
272 #[derive(Clone, Copy)]
274 impl Iterator for Iter {
276 fn next(&mut self) -> Option<Self::Item> {
280 fn takes_iter(_: impl Iterator) {}
281 fn dont_warn(mut x: Iter) {
285 fn warn(mut x: &mut Iter) {
292 #![clippy::msrv = "1.52.0"]
295 let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
301 #![clippy::msrv = "1.53.0"]
304 let _ = std::process::Command::new("ls").args(&["-a", "-l"]).status().unwrap();
310 // Should not lint because unions need explicit deref when accessing field
311 use std::mem::ManuallyDrop;
314 crab: ManuallyDrop<Vec<i32>>,
318 coral: ManuallyDrop<Coral>,
321 let mut ocean = Ocean {
322 coral: ManuallyDrop::new(Coral {
323 crab: ManuallyDrop::new(vec![1, 2, 3]),
328 ManuallyDrop::drop(&mut (&mut ocean.coral).crab);
330 (*ocean.coral).crab = ManuallyDrop::new(vec![4, 5, 6]);
331 ManuallyDrop::drop(&mut (*ocean.coral).crab);
333 ManuallyDrop::drop(&mut ocean.coral);
339 let env = "env".to_owned();
340 let arg = "arg".to_owned();
342 let loc = "loc".to_owned();
343 let _ = std::fs::write("x", &env); // Don't lint. In environment
344 let _ = std::fs::write("x", &arg);
345 let _ = std::fs::write("x", &loc);
347 let _ = std::fs::write("x", &env); // Don't lint. Borrowed by `f`
352 mod significant_drop {
360 fn drop(&mut self) {}
365 debug(&y); // Don't lint. Has significant drop
368 fn debug(_: impl std::fmt::Debug) {}
372 mod used_exactly_once {
376 fn use_x(_: impl AsRef<str>) {}
380 mod used_more_than_once {
385 fn use_x(_: impl AsRef<str>) {}
386 fn use_x_again(_: impl AsRef<str>) {}
389 // https://github.com/rust-lang/rust-clippy/issues/9111#issuecomment-1277114280
394 impl Extend<u8> for A {
395 fn extend<T: IntoIterator<Item = u8>>(&mut self, _: T) {
400 impl<'a> Extend<&'a u8> for A {
401 fn extend<T: IntoIterator<Item = &'a u8>>(&mut self, _: T) {
408 a.extend(&[]); // vs a.extend([]);
415 let string = String::new();
421 fn f<T: AsRef<str>>(_: T) {}
426 fn foo<D: std::fmt::Display>(_it: impl IntoIterator<Item = D>) {}
429 foo(if std::env::var_os("HI").is_some() {
438 mod issue_9739_method_variant {
442 fn foo<D: std::fmt::Display>(&self, _it: impl IntoIterator<Item = D>) {}
446 S.foo(if std::env::var_os("HI").is_some() {
456 fn foo<T: AsRef<[u8]>>(t: T) {
457 println!("{}", std::mem::size_of::<T>());
458 let _t: &[u8] = t.as_ref();
462 let a: [u8; 100] = [0u8; 100];
473 foo::<&[u8; 100]>(&a);
479 mod issue_9782_type_relative_variant {
483 fn foo<T: AsRef<[u8]>>(t: T) {
484 println!("{}", std::mem::size_of::<T>());
485 let _t: &[u8] = t.as_ref();
490 let a: [u8; 100] = [0u8; 100];
492 S::foo::<&[u8; 100]>(&a);
497 mod issue_9782_method_variant {
501 fn foo<T: AsRef<[u8]>>(&self, t: T) {
502 println!("{}", std::mem::size_of::<T>());
503 let _t: &[u8] = t.as_ref();
508 let a: [u8; 100] = [0u8; 100];
510 S.foo::<&[u8; 100]>(&a);