From 7f5486e4ec99128bde8ee94bbfa025b4df571277 Mon Sep 17 00:00:00 2001 From: tom-anders <13141438+tom-anders@users.noreply.github.com> Date: Mon, 4 Sep 2023 16:17:59 +0200 Subject: [PATCH] New lint: borrowed_option --- CHANGELOG.md | 1 + clippy_lints/src/declared_lints.rs | 1 + clippy_lints/src/types/borrowed_box.rs | 18 ++++--- clippy_lints/src/types/mod.rs | 34 ++++++++++++- tests/ui/borrow_box.rs | 26 +++++++--- tests/ui/borrow_box.stderr | 68 ++++++++++++++++++++------ tests/ui/clone_on_copy.fixed | 3 +- tests/ui/clone_on_copy.rs | 3 +- tests/ui/clone_on_copy.stderr | 18 +++---- tests/ui/if_same_then_else2.rs | 1 + tests/ui/if_same_then_else2.stderr | 24 ++++----- tests/ui/iter_overeager_cloned.fixed | 2 +- tests/ui/iter_overeager_cloned.rs | 2 +- tests/ui/option_if_let_else.fixed | 1 + tests/ui/option_if_let_else.rs | 1 + tests/ui/option_if_let_else.stderr | 46 ++++++++--------- tests/ui/partialeq_to_none.fixed | 2 +- tests/ui/partialeq_to_none.rs | 2 +- tests/ui/ref_option_ref.rs | 2 +- 19 files changed, 174 insertions(+), 81 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fd7232e280fc..cbcdf4149104 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4805,6 +4805,7 @@ Released 2018-09-13 [`borrow_deref_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_deref_ref [`borrow_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const [`borrowed_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box +[`borrowed_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_option [`box_collection`]: https://rust-lang.github.io/rust-clippy/master/index.html#box_collection [`box_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#box_default [`box_vec`]: https://rust-lang.github.io/rust-clippy/master/index.html#box_vec diff --git a/clippy_lints/src/declared_lints.rs b/clippy_lints/src/declared_lints.rs index 150d883985ce..098b80b72d88 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -650,6 +650,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[ crate::transmute::WRONG_TRANSMUTE_INFO, crate::tuple_array_conversions::TUPLE_ARRAY_CONVERSIONS_INFO, crate::types::BORROWED_BOX_INFO, + crate::types::BORROWED_OPTION_INFO, crate::types::BOX_COLLECTION_INFO, crate::types::LINKEDLIST_INFO, crate::types::OPTION_OPTION_INFO, diff --git a/clippy_lints/src/types/borrowed_box.rs b/clippy_lints/src/types/borrowed_box.rs index 306ca5724da1..3bf5193b7f6a 100644 --- a/clippy_lints/src/types/borrowed_box.rs +++ b/clippy_lints/src/types/borrowed_box.rs @@ -8,7 +8,7 @@ use rustc_hir::{ use rustc_lint::LateContext; use rustc_span::sym; -use super::BORROWED_BOX; +use super::{BORROWED_BOX, BORROWED_OPTION}; pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, mut_ty: &MutTy<'_>) -> bool { match mut_ty.ty.kind { @@ -17,7 +17,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m let def = cx.qpath_res(qpath, hir_id); if_chain! { if let Some(def_id) = def.opt_def_id(); - if Some(def_id) == cx.tcx.lang_items().owned_box(); + if Some(def_id) == cx.tcx.lang_items().owned_box() || Some(def_id) == cx.tcx.lang_items().option_type(); if let QPath::Resolved(None, path) = *qpath; if let [ref bx] = *path.segments; if let Some(params) = bx.args; @@ -27,7 +27,8 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m _ => None, }); then { - if is_any_trait(cx, inner) { + let is_box = Some(def_id) == cx.tcx.lang_items().owned_box(); + if is_box && is_any_trait(cx, inner) { // Ignore `Box` types; see issue #1884 for details. return false; } @@ -41,6 +42,7 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m if mut_ty.mutbl == Mutability::Mut { // Ignore `&mut Box` types; see issue #2907 for // details. + // Same reasoning applies for `&mut Option` types. return false; } @@ -62,11 +64,15 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m }; span_lint_and_sugg( cx, - BORROWED_BOX, + if is_box { BORROWED_BOX } else { BORROWED_OPTION }, hir_ty.span, - "you seem to be trying to use `&Box`. Consider using just `&T`", + if is_box { + "you seem to be trying to use `&Box`. Consider using just `&T`" + } else { + "you seem to be trying to use `&Option`. Consider using `Option<&T>` instead" + }, "try", - suggestion, + if is_box { suggestion } else { format!("Option<{suggestion}>") }, // To make this `MachineApplicable`, at least one needs to check if it isn't a trait item // because the trait impls of it will break otherwise; // and there may be other cases that result in invalid code. diff --git a/clippy_lints/src/types/mod.rs b/clippy_lints/src/types/mod.rs index 79f9d45d597e..dfa3e471afef 100644 --- a/clippy_lints/src/types/mod.rs +++ b/clippy_lints/src/types/mod.rs @@ -188,6 +188,38 @@ declare_clippy_lint! { "a borrow of a boxed type" } +declare_clippy_lint! { + /// ### What it does + /// Checks for usage of `&Option` anywhere in the code. + /// Check the [Option documentation](https://doc.rust-lang.org/std/option/index.html) for more information. + /// + /// ### Why is this bad? + /// A `&Option` parameter prevents calling the function if the caller holds a different type, e.g. `Result`. + /// Using `Option<&T>` generalizes the function, e.g. allowing to pass `res.ok().as_ref()` + /// Returning `&Option` needlessly exposes implementation details and has no advantage over `Option<&T>`. + /// + /// ### Example + /// ```rust,compile_fail + /// fn foo(bar: &Option) -> &Option { bar } + /// fn call_foo(bar: &Result) { + /// foo(bar.ok()); // does not work + /// } + /// ``` + /// + /// Better: + /// + /// ```rust + /// fn foo(bar: Option<&i32>) -> Option<&i32> { bar } + /// fn call_foo(bar: &Result) { + /// foo(bar.ok().as_ref()); // works! + /// } + /// ``` + #[clippy::version = "1.74.0"] + pub BORROWED_OPTION, + complexity, + "`&Option` instead of `Option<&T>`" +} + declare_clippy_lint! { /// ### What it does /// Checks for usage of redundant allocations anywhere in the code. @@ -309,7 +341,7 @@ pub struct Types { avoid_breaking_exported_api: bool, } -impl_lint_pass!(Types => [BOX_COLLECTION, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, REDUNDANT_ALLOCATION, RC_BUFFER, RC_MUTEX, TYPE_COMPLEXITY]); +impl_lint_pass!(Types => [BOX_COLLECTION, VEC_BOX, OPTION_OPTION, LINKEDLIST, BORROWED_BOX, BORROWED_OPTION, REDUNDANT_ALLOCATION, RC_BUFFER, RC_MUTEX, TYPE_COMPLEXITY]); impl<'tcx> LateLintPass<'tcx> for Types { fn check_fn( diff --git a/tests/ui/borrow_box.rs b/tests/ui/borrow_box.rs index e9994aac8454..51fefbebaaf2 100644 --- a/tests/ui/borrow_box.rs +++ b/tests/ui/borrow_box.rs @@ -9,7 +9,7 @@ use std::fmt::Display; -pub fn test1(foo: &mut Box) { +pub fn test1(foo: &mut Box, bar: &mut Option) { // Although this function could be changed to "&mut bool", // avoiding the Box, mutable references to boxes are not // flagged by this lint. @@ -18,26 +18,33 @@ pub fn test1(foo: &mut Box) { // the memory location of the pointed-to object could be // modified. By passing a mutable reference, the contents // could change, but not the location. - println!("{:?}", foo) + // + // The same reasoning applies to ignoring &mut Option + println!("{:?} {:?}", foo, bar) } pub fn test2() { let foo: &Box; //~^ ERROR: you seem to be trying to use `&Box`. Consider using just `&T` + let bar: &Option; + //~^ ERROR: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead } struct Test3<'a> { foo: &'a Box, //~^ ERROR: you seem to be trying to use `&Box`. Consider using just `&T` + bar: &'a Option, + //~^ ERROR: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead } trait Test4 { - fn test4(a: &Box); + fn test4(a: &Box, b: &Option); //~^ ERROR: you seem to be trying to use `&Box`. Consider using just `&T` + //~| ERROR: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead } impl<'a> Test4 for Test3<'a> { - fn test4(a: &Box) { + fn test4(a: &Box, b: &Option) { unimplemented!(); } } @@ -106,12 +113,15 @@ pub fn test15(_display: &Box) {} pub fn test16<'a>(_display: &'a Box) {} //~^ ERROR: you seem to be trying to use `&Box`. Consider using just `&T` -pub fn test17(_display: &Box) {} +pub fn test17(_display: &Box, _display2: &Option) {} //~^ ERROR: you seem to be trying to use `&Box`. Consider using just `&T` -pub fn test18(_display: &Box) {} +//~| ERROR: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead +pub fn test18(_display: &Box, _display2: &Option) {} //~^ ERROR: you seem to be trying to use `&Box`. Consider using just `&T` -pub fn test19<'a>(_display: &'a Box) {} +//~| ERROR: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead +pub fn test19<'a>(_display: &'a Box, _display2: &'a Option) {} //~^ ERROR: you seem to be trying to use `&Box`. Consider using just `&T` +//~| ERROR: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead // This exists only to check what happens when parentheses are already present. // Even though the current implementation doesn't put extra parentheses, @@ -120,7 +130,7 @@ pub fn test20(_display: &Box<(dyn Display + Send)>) {} //~^ ERROR: you seem to be trying to use `&Box`. Consider using just `&T` fn main() { - test1(&mut Box::new(false)); + test1(&mut Box::new(false), &mut Some(false)); test2(); test5(&mut (Box::new(false) as Box)); test6(); diff --git a/tests/ui/borrow_box.stderr b/tests/ui/borrow_box.stderr index a9773958a377..80d8205c4b3b 100644 --- a/tests/ui/borrow_box.stderr +++ b/tests/ui/borrow_box.stderr @@ -1,5 +1,5 @@ error: you seem to be trying to use `&Box`. Consider using just `&T` - --> $DIR/borrow_box.rs:25:14 + --> $DIR/borrow_box.rs:27:14 | LL | let foo: &Box; | ^^^^^^^^^^ help: try: `&bool` @@ -10,59 +10,97 @@ note: the lint level is defined here LL | #![deny(clippy::borrowed_box)] | ^^^^^^^^^^^^^^^^^^^^ +error: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead + --> $DIR/borrow_box.rs:29:14 + | +LL | let bar: &Option; + | ^^^^^^^^^^^^^ help: try: `Option<&bool>` + | + = note: `-D clippy::borrowed-option` implied by `-D warnings` + error: you seem to be trying to use `&Box`. Consider using just `&T` - --> $DIR/borrow_box.rs:30:10 + --> $DIR/borrow_box.rs:34:10 | LL | foo: &'a Box, | ^^^^^^^^^^^^^ help: try: `&'a bool` +error: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead + --> $DIR/borrow_box.rs:36:10 + | +LL | bar: &'a Option, + | ^^^^^^^^^^^^^^^^ help: try: `Option<&'a bool>` + error: you seem to be trying to use `&Box`. Consider using just `&T` - --> $DIR/borrow_box.rs:35:17 + --> $DIR/borrow_box.rs:41:17 | -LL | fn test4(a: &Box); +LL | fn test4(a: &Box, b: &Option); | ^^^^^^^^^^ help: try: `&bool` +error: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead + --> $DIR/borrow_box.rs:41:32 + | +LL | fn test4(a: &Box, b: &Option); + | ^^^^^^^^^^^^^ help: try: `Option<&bool>` + error: you seem to be trying to use `&Box`. Consider using just `&T` - --> $DIR/borrow_box.rs:102:25 + --> $DIR/borrow_box.rs:109:25 | LL | pub fn test14(_display: &Box) {} | ^^^^^^^^^^^^^^^^^ help: try: `&dyn Display` error: you seem to be trying to use `&Box`. Consider using just `&T` - --> $DIR/borrow_box.rs:104:25 + --> $DIR/borrow_box.rs:111:25 | LL | pub fn test15(_display: &Box) {} | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&(dyn Display + Send)` error: you seem to be trying to use `&Box`. Consider using just `&T` - --> $DIR/borrow_box.rs:106:29 + --> $DIR/borrow_box.rs:113:29 | LL | pub fn test16<'a>(_display: &'a Box) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&'a (dyn Display + 'a)` error: you seem to be trying to use `&Box`. Consider using just `&T` - --> $DIR/borrow_box.rs:109:25 + --> $DIR/borrow_box.rs:116:25 | -LL | pub fn test17(_display: &Box) {} +LL | pub fn test17(_display: &Box, _display2: &Option) {} | ^^^^^^^^^^^^^^^^^^ help: try: `&impl Display` +error: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead + --> $DIR/borrow_box.rs:116:56 + | +LL | pub fn test17(_display: &Box, _display2: &Option) {} + | ^^^^^^^^^^^^^^^^^^^^^ help: try: `Option<&impl Display>` + error: you seem to be trying to use `&Box`. Consider using just `&T` - --> $DIR/borrow_box.rs:111:25 + --> $DIR/borrow_box.rs:119:25 | -LL | pub fn test18(_display: &Box) {} +LL | pub fn test18(_display: &Box, _display2: &Option) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&(impl Display + Send)` +error: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead + --> $DIR/borrow_box.rs:119:63 + | +LL | pub fn test18(_display: &Box, _display2: &Option) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Option<&(impl Display + Send)>` + error: you seem to be trying to use `&Box`. Consider using just `&T` - --> $DIR/borrow_box.rs:113:29 + --> $DIR/borrow_box.rs:122:29 | -LL | pub fn test19<'a>(_display: &'a Box) {} +LL | pub fn test19<'a>(_display: &'a Box, _display2: &'a Option) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&'a (impl Display + 'a)` +error: you seem to be trying to use `&Option`. Consider using `Option<&T>` instead + --> $DIR/borrow_box.rs:122:68 + | +LL | pub fn test19<'a>(_display: &'a Box, _display2: &'a Option) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Option<&'a (impl Display + 'a)>` + error: you seem to be trying to use `&Box`. Consider using just `&T` - --> $DIR/borrow_box.rs:119:25 + --> $DIR/borrow_box.rs:129:25 | LL | pub fn test20(_display: &Box<(dyn Display + Send)>) {} | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `&(dyn Display + Send)` -error: aborting due to 10 previous errors +error: aborting due to 16 previous errors diff --git a/tests/ui/clone_on_copy.fixed b/tests/ui/clone_on_copy.fixed index 9d9a5bf20f43..bda10d47cbd3 100644 --- a/tests/ui/clone_on_copy.fixed +++ b/tests/ui/clone_on_copy.fixed @@ -6,7 +6,8 @@ clippy::unnecessary_operation, clippy::vec_init_then_push, clippy::toplevel_ref_arg, - clippy::needless_borrow + clippy::needless_borrow, + clippy::borrowed_option )] use std::cell::RefCell; diff --git a/tests/ui/clone_on_copy.rs b/tests/ui/clone_on_copy.rs index 305bc6816e1b..25cc27784083 100644 --- a/tests/ui/clone_on_copy.rs +++ b/tests/ui/clone_on_copy.rs @@ -6,7 +6,8 @@ clippy::unnecessary_operation, clippy::vec_init_then_push, clippy::toplevel_ref_arg, - clippy::needless_borrow + clippy::needless_borrow, + clippy::borrowed_option )] use std::cell::RefCell; diff --git a/tests/ui/clone_on_copy.stderr b/tests/ui/clone_on_copy.stderr index 053dee954481..967b9ccd7046 100644 --- a/tests/ui/clone_on_copy.stderr +++ b/tests/ui/clone_on_copy.stderr @@ -1,5 +1,5 @@ error: using `clone` on type `i32` which implements the `Copy` trait - --> $DIR/clone_on_copy.rs:23:5 + --> $DIR/clone_on_copy.rs:24:5 | LL | 42.clone(); | ^^^^^^^^^^ help: try removing the `clone` call: `42` @@ -7,49 +7,49 @@ LL | 42.clone(); = note: `-D clippy::clone-on-copy` implied by `-D warnings` error: using `clone` on type `i32` which implements the `Copy` trait - --> $DIR/clone_on_copy.rs:27:5 + --> $DIR/clone_on_copy.rs:28:5 | LL | (&42).clone(); | ^^^^^^^^^^^^^ help: try dereferencing it: `*(&42)` error: using `clone` on type `i32` which implements the `Copy` trait - --> $DIR/clone_on_copy.rs:30:5 + --> $DIR/clone_on_copy.rs:31:5 | LL | rc.borrow().clone(); | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*rc.borrow()` error: using `clone` on type `u32` which implements the `Copy` trait - --> $DIR/clone_on_copy.rs:33:5 + --> $DIR/clone_on_copy.rs:34:5 | LL | x.clone().rotate_left(1); | ^^^^^^^^^ help: try removing the `clone` call: `x` error: using `clone` on type `i32` which implements the `Copy` trait - --> $DIR/clone_on_copy.rs:47:5 + --> $DIR/clone_on_copy.rs:48:5 | LL | m!(42).clone(); | ^^^^^^^^^^^^^^ help: try removing the `clone` call: `m!(42)` error: using `clone` on type `[u32; 2]` which implements the `Copy` trait - --> $DIR/clone_on_copy.rs:57:5 + --> $DIR/clone_on_copy.rs:58:5 | LL | x.clone()[0]; | ^^^^^^^^^ help: try dereferencing it: `(*x)` error: using `clone` on type `char` which implements the `Copy` trait - --> $DIR/clone_on_copy.rs:67:14 + --> $DIR/clone_on_copy.rs:68:14 | LL | is_ascii('z'.clone()); | ^^^^^^^^^^^ help: try removing the `clone` call: `'z'` error: using `clone` on type `i32` which implements the `Copy` trait - --> $DIR/clone_on_copy.rs:71:14 + --> $DIR/clone_on_copy.rs:72:14 | LL | vec.push(42.clone()); | ^^^^^^^^^^ help: try removing the `clone` call: `42` error: using `clone` on type `Option` which implements the `Copy` trait - --> $DIR/clone_on_copy.rs:75:17 + --> $DIR/clone_on_copy.rs:76:17 | LL | let value = opt.clone()?; // operator precedence needed (*opt)? | ^^^^^^^^^^^ help: try dereferencing it: `(*opt)` diff --git a/tests/ui/if_same_then_else2.rs b/tests/ui/if_same_then_else2.rs index 0b171f21d0cc..dd9f550df71c 100644 --- a/tests/ui/if_same_then_else2.rs +++ b/tests/ui/if_same_then_else2.rs @@ -7,6 +7,7 @@ clippy::ifs_same_cond, clippy::needless_if, clippy::needless_return, + clippy::borrowed_option, clippy::single_element_loop, clippy::branches_sharing_code )] diff --git a/tests/ui/if_same_then_else2.stderr b/tests/ui/if_same_then_else2.stderr index 56e5f3e45b22..74f1e106ddee 100644 --- a/tests/ui/if_same_then_else2.stderr +++ b/tests/ui/if_same_then_else2.stderr @@ -1,5 +1,5 @@ error: this `if` has identical blocks - --> $DIR/if_same_then_else2.rs:15:13 + --> $DIR/if_same_then_else2.rs:16:13 | LL | if true { | _____________^ @@ -12,7 +12,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/if_same_then_else2.rs:25:12 + --> $DIR/if_same_then_else2.rs:26:12 | LL | } else { | ____________^ @@ -26,7 +26,7 @@ LL | | } = note: `-D clippy::if-same-then-else` implied by `-D warnings` error: this `if` has identical blocks - --> $DIR/if_same_then_else2.rs:36:13 + --> $DIR/if_same_then_else2.rs:37:13 | LL | if true { | _____________^ @@ -36,7 +36,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/if_same_then_else2.rs:39:12 + --> $DIR/if_same_then_else2.rs:40:12 | LL | } else { | ____________^ @@ -45,7 +45,7 @@ LL | | } | |_____^ error: this `if` has identical blocks - --> $DIR/if_same_then_else2.rs:43:13 + --> $DIR/if_same_then_else2.rs:44:13 | LL | if true { | _____________^ @@ -55,7 +55,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/if_same_then_else2.rs:46:12 + --> $DIR/if_same_then_else2.rs:47:12 | LL | } else { | ____________^ @@ -64,7 +64,7 @@ LL | | } | |_____^ error: this `if` has identical blocks - --> $DIR/if_same_then_else2.rs:93:21 + --> $DIR/if_same_then_else2.rs:94:21 | LL | let _ = if true { | _____________________^ @@ -74,7 +74,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/if_same_then_else2.rs:96:12 + --> $DIR/if_same_then_else2.rs:97:12 | LL | } else { | ____________^ @@ -83,7 +83,7 @@ LL | | }; | |_____^ error: this `if` has identical blocks - --> $DIR/if_same_then_else2.rs:100:13 + --> $DIR/if_same_then_else2.rs:101:13 | LL | if true { | _____________^ @@ -93,7 +93,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/if_same_then_else2.rs:103:12 + --> $DIR/if_same_then_else2.rs:104:12 | LL | } else { | ____________^ @@ -102,7 +102,7 @@ LL | | } | |_____^ error: this `if` has identical blocks - --> $DIR/if_same_then_else2.rs:124:20 + --> $DIR/if_same_then_else2.rs:125:20 | LL | } else if true { | ____________________^ @@ -113,7 +113,7 @@ LL | | } else { | |_____^ | note: same as this - --> $DIR/if_same_then_else2.rs:128:12 + --> $DIR/if_same_then_else2.rs:129:12 | LL | } else { | ____________^ diff --git a/tests/ui/iter_overeager_cloned.fixed b/tests/ui/iter_overeager_cloned.fixed index 7d8a584b0224..47c0dfdfe3cf 100644 --- a/tests/ui/iter_overeager_cloned.fixed +++ b/tests/ui/iter_overeager_cloned.fixed @@ -1,5 +1,5 @@ #![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)] -#![allow(dead_code, clippy::let_unit_value, clippy::useless_vec)] +#![allow(dead_code, clippy::let_unit_value, clippy::useless_vec, clippy::borrowed_option)] fn main() { let vec = vec!["1".to_string(), "2".to_string(), "3".to_string()]; diff --git a/tests/ui/iter_overeager_cloned.rs b/tests/ui/iter_overeager_cloned.rs index 58c374ab8cd1..a7fc1a975e01 100644 --- a/tests/ui/iter_overeager_cloned.rs +++ b/tests/ui/iter_overeager_cloned.rs @@ -1,5 +1,5 @@ #![warn(clippy::iter_overeager_cloned, clippy::redundant_clone, clippy::filter_next)] -#![allow(dead_code, clippy::let_unit_value, clippy::useless_vec)] +#![allow(dead_code, clippy::let_unit_value, clippy::useless_vec, clippy::borrowed_option)] fn main() { let vec = vec!["1".to_string(), "2".to_string(), "3".to_string()]; diff --git a/tests/ui/option_if_let_else.fixed b/tests/ui/option_if_let_else.fixed index c3415a7df9e3..39b671d4d605 100644 --- a/tests/ui/option_if_let_else.fixed +++ b/tests/ui/option_if_let_else.fixed @@ -3,6 +3,7 @@ unused_tuple_struct_fields, clippy::redundant_closure, clippy::ref_option_ref, + clippy::borrowed_option, clippy::equatable_if_let, clippy::let_unit_value, clippy::redundant_locals diff --git a/tests/ui/option_if_let_else.rs b/tests/ui/option_if_let_else.rs index 86537f62057b..913841b97118 100644 --- a/tests/ui/option_if_let_else.rs +++ b/tests/ui/option_if_let_else.rs @@ -3,6 +3,7 @@ unused_tuple_struct_fields, clippy::redundant_closure, clippy::ref_option_ref, + clippy::borrowed_option, clippy::equatable_if_let, clippy::let_unit_value, clippy::redundant_locals diff --git a/tests/ui/option_if_let_else.stderr b/tests/ui/option_if_let_else.stderr index aa2da2174003..350f0f07e136 100644 --- a/tests/ui/option_if_let_else.stderr +++ b/tests/ui/option_if_let_else.stderr @@ -1,5 +1,5 @@ error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:12:5 + --> $DIR/option_if_let_else.rs:13:5 | LL | / if let Some(x) = string { LL | | (true, x) @@ -11,19 +11,19 @@ LL | | } = note: `-D clippy::option-if-let-else` implied by `-D warnings` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:30:13 + --> $DIR/option_if_let_else.rs:31:13 | LL | let _ = if let Some(s) = *string { s.len() } else { 0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `string.map_or(0, |s| s.len())` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:31:13 + --> $DIR/option_if_let_else.rs:32:13 | LL | let _ = if let Some(s) = &num { s } else { &0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.as_ref().map_or(&0, |s| s)` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:32:13 + --> $DIR/option_if_let_else.rs:33:13 | LL | let _ = if let Some(s) = &mut num { | _____________^ @@ -43,13 +43,13 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:38:13 + --> $DIR/option_if_let_else.rs:39:13 | LL | let _ = if let Some(ref s) = num { s } else { &0 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `num.as_ref().map_or(&0, |s| s)` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:39:13 + --> $DIR/option_if_let_else.rs:40:13 | LL | let _ = if let Some(mut s) = num { | _____________^ @@ -69,7 +69,7 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:45:13 + --> $DIR/option_if_let_else.rs:46:13 | LL | let _ = if let Some(ref mut s) = num { | _____________^ @@ -89,7 +89,7 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:54:5 + --> $DIR/option_if_let_else.rs:55:5 | LL | / if let Some(x) = arg { LL | | let y = x * x; @@ -108,7 +108,7 @@ LL + }) | error: use Option::map_or_else instead of an if let/else - --> $DIR/option_if_let_else.rs:67:13 + --> $DIR/option_if_let_else.rs:68:13 | LL | let _ = if let Some(x) = arg { | _____________^ @@ -120,7 +120,7 @@ LL | | }; | |_____^ help: try: `arg.map_or_else(|| side_effect(), |x| x)` error: use Option::map_or_else instead of an if let/else - --> $DIR/option_if_let_else.rs:76:13 + --> $DIR/option_if_let_else.rs:77:13 | LL | let _ = if let Some(x) = arg { | _____________^ @@ -143,7 +143,7 @@ LL ~ }, |x| x * x * x * x); | error: use Option::map_or_else instead of an if let/else - --> $DIR/option_if_let_else.rs:109:13 + --> $DIR/option_if_let_else.rs:110:13 | LL | / if let Some(idx) = s.find('.') { LL | | vec![s[..idx].to_string(), s[idx..].to_string()] @@ -153,7 +153,7 @@ LL | | } | |_____________^ help: try: `s.find('.').map_or_else(|| vec![s.to_string()], |idx| vec![s[..idx].to_string(), s[idx..].to_string()])` error: use Option::map_or_else instead of an if let/else - --> $DIR/option_if_let_else.rs:120:5 + --> $DIR/option_if_let_else.rs:121:5 | LL | / if let Ok(binding) = variable { LL | | println!("Ok {binding}"); @@ -172,13 +172,13 @@ LL + }) | error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:142:13 + --> $DIR/option_if_let_else.rs:143:13 | LL | let _ = if let Some(x) = optional { x + 2 } else { 5 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `optional.map_or(5, |x| x + 2)` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:152:13 + --> $DIR/option_if_let_else.rs:153:13 | LL | let _ = if let Some(x) = Some(0) { | _____________^ @@ -200,13 +200,13 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:180:13 + --> $DIR/option_if_let_else.rs:181:13 | LL | let _ = if let Some(x) = Some(0) { s.len() + x } else { s.len() }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `Some(0).map_or(s.len(), |x| s.len() + x)` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:184:13 + --> $DIR/option_if_let_else.rs:185:13 | LL | let _ = if let Some(x) = Some(0) { | _____________^ @@ -226,7 +226,7 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:223:13 + --> $DIR/option_if_let_else.rs:224:13 | LL | let _ = match s { | _____________^ @@ -236,7 +236,7 @@ LL | | }; | |_____^ help: try: `s.map_or(1, |string| string.len())` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:227:13 + --> $DIR/option_if_let_else.rs:228:13 | LL | let _ = match Some(10) { | _____________^ @@ -246,7 +246,7 @@ LL | | }; | |_____^ help: try: `Some(10).map_or(5, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:233:13 + --> $DIR/option_if_let_else.rs:234:13 | LL | let _ = match res { | _____________^ @@ -256,7 +256,7 @@ LL | | }; | |_____^ help: try: `res.map_or(1, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:237:13 + --> $DIR/option_if_let_else.rs:238:13 | LL | let _ = match res { | _____________^ @@ -266,13 +266,13 @@ LL | | }; | |_____^ help: try: `res.map_or(1, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:241:13 + --> $DIR/option_if_let_else.rs:242:13 | LL | let _ = if let Ok(a) = res { a + 1 } else { 5 }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `res.map_or(5, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:258:9 + --> $DIR/option_if_let_else.rs:259:9 | LL | / match initial { LL | | Some(value) => do_something(value), @@ -281,7 +281,7 @@ LL | | } | |_________^ help: try: `initial.as_ref().map_or({}, |value| do_something(value))` error: use Option::map_or instead of an if let/else - --> $DIR/option_if_let_else.rs:265:9 + --> $DIR/option_if_let_else.rs:266:9 | LL | / match initial { LL | | Some(value) => do_something2(value), diff --git a/tests/ui/partialeq_to_none.fixed b/tests/ui/partialeq_to_none.fixed index 87adbca39484..069f669a1545 100644 --- a/tests/ui/partialeq_to_none.fixed +++ b/tests/ui/partialeq_to_none.fixed @@ -1,5 +1,5 @@ #![warn(clippy::partialeq_to_none)] -#![allow(clippy::eq_op, clippy::needless_if)] +#![allow(clippy::eq_op, clippy::borrowed_option, clippy::needless_if)] struct Foobar; diff --git a/tests/ui/partialeq_to_none.rs b/tests/ui/partialeq_to_none.rs index b623e6a66268..da4da417ce8a 100644 --- a/tests/ui/partialeq_to_none.rs +++ b/tests/ui/partialeq_to_none.rs @@ -1,5 +1,5 @@ #![warn(clippy::partialeq_to_none)] -#![allow(clippy::eq_op, clippy::needless_if)] +#![allow(clippy::eq_op, clippy::borrowed_option, clippy::needless_if)] struct Foobar; diff --git a/tests/ui/ref_option_ref.rs b/tests/ui/ref_option_ref.rs index 44001c45e99a..40579c7edcd2 100644 --- a/tests/ui/ref_option_ref.rs +++ b/tests/ui/ref_option_ref.rs @@ -1,4 +1,4 @@ -#![allow(unused)] +#![allow(unused, clippy::borrowed_option)] #![warn(clippy::ref_option_ref)] //@no-rustfix // This lint is not tagged as run-rustfix because automatically