diff --git a/CHANGELOG.md b/CHANGELOG.md index 87bffe7f74d6..650a328f91f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5121,6 +5121,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 eba048ad90be..7924f31b245f 100644 --- a/clippy_lints/src/declared_lints.rs +++ b/clippy_lints/src/declared_lints.rs @@ -697,6 +697,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 801e88626199..8a325b7fcfc7 100644 --- a/clippy_lints/src/types/borrowed_box.rs +++ b/clippy_lints/src/types/borrowed_box.rs @@ -7,7 +7,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 { @@ -15,7 +15,8 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m let hir_id = mut_ty.ty.hir_id; let def = cx.qpath_res(qpath, hir_id); if let Some(def_id) = def.opt_def_id() - && Some(def_id) == cx.tcx.lang_items().owned_box() + && (Some(def_id) == cx.tcx.lang_items().owned_box() + || Some(def_id) == cx.tcx.lang_items().option_type()) && let QPath::Resolved(None, path) = *qpath && let [ref bx] = *path.segments && let Some(params) = bx.args @@ -25,7 +26,8 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m _ => None, }) { - 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; } @@ -39,6 +41,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; } @@ -60,11 +63,19 @@ 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 2ad15ac8312d..4ee5bbe5bfec 100644 --- a/clippy_lints/src/types/mod.rs +++ b/clippy_lints/src/types/mod.rs @@ -188,6 +188,37 @@ declare_clippy_lint! { "a borrow of a boxed type" } +declare_clippy_lint! { + /// ### What it does + /// Checks for usage of [`&Option`](https://doc.rust-lang.org/std/option/index.html) anywhere in the code. + /// + /// ### Why is this bad? + /// An `&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 + /// } + /// ``` + /// + /// Use instead: + /// + /// ```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 +340,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 34e8f11dfd74..5ad0abc5f597 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` - --> tests/ui/borrow_box.rs:25:14 + --> tests/ui/borrow_box.rs:27:14 | LL | let foo: &Box; | ^^^^^^^^^^ help: try: `&bool` @@ -10,59 +10,98 @@ 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 + --> tests/ui/borrow_box.rs:29:14 + | +LL | let bar: &Option; + | ^^^^^^^^^^^^^ help: try: `Option<&bool>` + | + = note: `-D clippy::borrowed-option` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::borrowed_option)]` + error: you seem to be trying to use `&Box`. Consider using just `&T` - --> tests/ui/borrow_box.rs:30:10 + --> tests/ui/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 + --> tests/ui/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` - --> tests/ui/borrow_box.rs:35:17 + --> tests/ui/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 + --> tests/ui/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` - --> tests/ui/borrow_box.rs:102:25 + --> tests/ui/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` - --> tests/ui/borrow_box.rs:104:25 + --> tests/ui/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` - --> tests/ui/borrow_box.rs:106:29 + --> tests/ui/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` - --> tests/ui/borrow_box.rs:109:25 + --> tests/ui/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 + --> tests/ui/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` - --> tests/ui/borrow_box.rs:111:25 + --> tests/ui/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 + --> tests/ui/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` - --> tests/ui/borrow_box.rs:113:29 + --> tests/ui/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 + --> tests/ui/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` - --> tests/ui/borrow_box.rs:119:25 + --> tests/ui/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 314fd13afca4..c9e92f787bd9 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 - --> tests/ui/clone_on_copy.rs:23:5 + --> tests/ui/clone_on_copy.rs:24:5 | LL | 42.clone(); | ^^^^^^^^^^ help: try removing the `clone` call: `42` @@ -8,49 +8,49 @@ LL | 42.clone(); = help: to override `-D warnings` add `#[allow(clippy::clone_on_copy)]` error: using `clone` on type `i32` which implements the `Copy` trait - --> tests/ui/clone_on_copy.rs:27:5 + --> tests/ui/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 - --> tests/ui/clone_on_copy.rs:30:5 + --> tests/ui/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 - --> tests/ui/clone_on_copy.rs:33:5 + --> tests/ui/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 - --> tests/ui/clone_on_copy.rs:47:5 + --> tests/ui/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 - --> tests/ui/clone_on_copy.rs:57:5 + --> tests/ui/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 - --> tests/ui/clone_on_copy.rs:67:14 + --> tests/ui/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 - --> tests/ui/clone_on_copy.rs:71:14 + --> tests/ui/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 - --> tests/ui/clone_on_copy.rs:75:17 + --> tests/ui/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 e23c77b08274..bce073e12c6c 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 93507eb2c6fd..0e215671d267 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 - --> tests/ui/if_same_then_else2.rs:15:13 + --> tests/ui/if_same_then_else2.rs:16:13 | LL | if true { | _____________^ @@ -12,7 +12,7 @@ LL | | } else { | |_____^ | note: same as this - --> tests/ui/if_same_then_else2.rs:24:12 + --> tests/ui/if_same_then_else2.rs:25:12 | LL | } else { | ____________^ @@ -27,7 +27,7 @@ LL | | } = help: to override `-D warnings` add `#[allow(clippy::if_same_then_else)]` error: this `if` has identical blocks - --> tests/ui/if_same_then_else2.rs:36:13 + --> tests/ui/if_same_then_else2.rs:37:13 | LL | if true { | _____________^ @@ -36,7 +36,7 @@ LL | | } else { | |_____^ | note: same as this - --> tests/ui/if_same_then_else2.rs:38:12 + --> tests/ui/if_same_then_else2.rs:39:12 | LL | } else { | ____________^ @@ -45,7 +45,7 @@ LL | | } | |_____^ error: this `if` has identical blocks - --> tests/ui/if_same_then_else2.rs:43:13 + --> tests/ui/if_same_then_else2.rs:44:13 | LL | if true { | _____________^ @@ -54,7 +54,7 @@ LL | | } else { | |_____^ | note: same as this - --> tests/ui/if_same_then_else2.rs:45:12 + --> tests/ui/if_same_then_else2.rs:46:12 | LL | } else { | ____________^ @@ -63,19 +63,19 @@ LL | | } | |_____^ error: this `if` has identical blocks - --> tests/ui/if_same_then_else2.rs:93:21 + --> tests/ui/if_same_then_else2.rs:94:21 | LL | let _ = if true { f32::NAN } else { f32::NAN }; | ^^^^^^^^^^^^ | note: same as this - --> tests/ui/if_same_then_else2.rs:93:39 + --> tests/ui/if_same_then_else2.rs:94:39 | LL | let _ = if true { f32::NAN } else { f32::NAN }; | ^^^^^^^^^^^^ error: this `if` has identical blocks - --> tests/ui/if_same_then_else2.rs:96:13 + --> tests/ui/if_same_then_else2.rs:97:13 | LL | if true { | _____________^ @@ -84,7 +84,7 @@ LL | | } else { | |_____^ | note: same as this - --> tests/ui/if_same_then_else2.rs:98:12 + --> tests/ui/if_same_then_else2.rs:99:12 | LL | } else { | ____________^ @@ -93,7 +93,7 @@ LL | | } | |_____^ error: this `if` has identical blocks - --> tests/ui/if_same_then_else2.rs:120:20 + --> tests/ui/if_same_then_else2.rs:121:20 | LL | } else if true { | ____________________^ @@ -103,7 +103,7 @@ LL | | } else { | |_____^ | note: same as this - --> tests/ui/if_same_then_else2.rs:123:12 + --> tests/ui/if_same_then_else2.rs:124:12 | LL | } else { | ____________^ diff --git a/tests/ui/iter_filter_is_some.fixed b/tests/ui/iter_filter_is_some.fixed index 8a818c0c6728..9f960bbfe73b 100644 --- a/tests/ui/iter_filter_is_some.fixed +++ b/tests/ui/iter_filter_is_some.fixed @@ -4,6 +4,7 @@ clippy::result_filter_map, clippy::needless_borrow, clippy::option_filter_map, + clippy::borrowed_option, clippy::redundant_closure, clippy::unnecessary_get_then_check )] diff --git a/tests/ui/iter_filter_is_some.rs b/tests/ui/iter_filter_is_some.rs index 9eda93a25921..eb29884a872e 100644 --- a/tests/ui/iter_filter_is_some.rs +++ b/tests/ui/iter_filter_is_some.rs @@ -4,6 +4,7 @@ clippy::result_filter_map, clippy::needless_borrow, clippy::option_filter_map, + clippy::borrowed_option, clippy::redundant_closure, clippy::unnecessary_get_then_check )] diff --git a/tests/ui/iter_filter_is_some.stderr b/tests/ui/iter_filter_is_some.stderr index 54aff892b1f0..f41725142fbf 100644 --- a/tests/ui/iter_filter_is_some.stderr +++ b/tests/ui/iter_filter_is_some.stderr @@ -1,5 +1,5 @@ error: `filter` for `is_some` on iterator over `Option` - --> tests/ui/iter_filter_is_some.rs:15:58 + --> tests/ui/iter_filter_is_some.rs:16:58 | LL | let _ = vec![Some(1), None, Some(3)].into_iter().filter(Option::is_some); | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` @@ -8,55 +8,55 @@ LL | let _ = vec![Some(1), None, Some(3)].into_iter().filter(Option::is_ = help: to override `-D warnings` add `#[allow(clippy::iter_filter_is_some)]` error: `filter` for `is_some` on iterator over `Option` - --> tests/ui/iter_filter_is_some.rs:17:58 + --> tests/ui/iter_filter_is_some.rs:18:58 | LL | let _ = vec![Some(1), None, Some(3)].into_iter().filter(|a| a.is_some()); | ^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` error: `filter` for `is_some` on iterator over `Option` - --> tests/ui/iter_filter_is_some.rs:20:58 + --> tests/ui/iter_filter_is_some.rs:21:58 | LL | let _ = vec![Some(1), None, Some(3)].into_iter().filter(|o| { o.is_some() }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` error: `filter` for `is_some` on iterator over `Option` - --> tests/ui/iter_filter_is_some.rs:27:14 + --> tests/ui/iter_filter_is_some.rs:28:14 | LL | .filter(std::option::Option::is_some); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` error: `filter` for `is_some` on iterator over `Option` - --> tests/ui/iter_filter_is_some.rs:32:14 + --> tests/ui/iter_filter_is_some.rs:33:14 | LL | .filter(|a| std::option::Option::is_some(a)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` error: `filter` for `is_some` on iterator over `Option` - --> tests/ui/iter_filter_is_some.rs:35:58 + --> tests/ui/iter_filter_is_some.rs:36:58 | LL | let _ = vec![Some(1), None, Some(3)].into_iter().filter(|a| { std::option::Option::is_some(a) }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` error: `filter` for `is_some` on iterator over `Option` - --> tests/ui/iter_filter_is_some.rs:40:58 + --> tests/ui/iter_filter_is_some.rs:41:58 | LL | let _ = vec![Some(1), None, Some(3)].into_iter().filter(|&a| a.is_some()); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` error: `filter` for `is_some` on iterator over `Option` - --> tests/ui/iter_filter_is_some.rs:44:58 + --> tests/ui/iter_filter_is_some.rs:45:58 | LL | let _ = vec![Some(1), None, Some(3)].into_iter().filter(|&o| { o.is_some() }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` error: `filter` for `is_some` on iterator over `Option` - --> tests/ui/iter_filter_is_some.rs:49:58 + --> tests/ui/iter_filter_is_some.rs:50:58 | LL | let _ = vec![Some(1), None, Some(3)].into_iter().filter(|ref a| a.is_some()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` error: `filter` for `is_some` on iterator over `Option` - --> tests/ui/iter_filter_is_some.rs:53:58 + --> tests/ui/iter_filter_is_some.rs:54:58 | LL | let _ = vec![Some(1), None, Some(3)].into_iter().filter(|ref o| { o.is_some() }); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `flatten` instead: `flatten()` 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 eeab801b7da8..dae484afb2dd 100644 --- a/tests/ui/option_if_let_else.fixed +++ b/tests/ui/option_if_let_else.fixed @@ -1,6 +1,7 @@ #![warn(clippy::option_if_let_else)] #![allow( 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 3e5b96d7c316..35ef1c29b750 100644 --- a/tests/ui/option_if_let_else.rs +++ b/tests/ui/option_if_let_else.rs @@ -1,6 +1,7 @@ #![warn(clippy::option_if_let_else)] #![allow( 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 f5359a0c34f9..37ef791edb00 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 - --> tests/ui/option_if_let_else.rs:11:5 + --> tests/ui/option_if_let_else.rs:12:5 | LL | / if let Some(x) = string { LL | | (true, x) @@ -12,19 +12,19 @@ LL | | } = help: to override `-D warnings` add `#[allow(clippy::option_if_let_else)]` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:29:13 + --> tests/ui/option_if_let_else.rs:30: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 - --> tests/ui/option_if_let_else.rs:30:13 + --> tests/ui/option_if_let_else.rs:31: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 - --> tests/ui/option_if_let_else.rs:31:13 + --> tests/ui/option_if_let_else.rs:32:13 | LL | let _ = if let Some(s) = &mut num { | _____________^ @@ -44,13 +44,13 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:37:13 + --> tests/ui/option_if_let_else.rs:38: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 - --> tests/ui/option_if_let_else.rs:38:13 + --> tests/ui/option_if_let_else.rs:39:13 | LL | let _ = if let Some(mut s) = num { | _____________^ @@ -70,7 +70,7 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:44:13 + --> tests/ui/option_if_let_else.rs:45:13 | LL | let _ = if let Some(ref mut s) = num { | _____________^ @@ -90,7 +90,7 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:53:5 + --> tests/ui/option_if_let_else.rs:54:5 | LL | / if let Some(x) = arg { LL | | let y = x * x; @@ -109,7 +109,7 @@ LL + }) | error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:66:13 + --> tests/ui/option_if_let_else.rs:67:13 | LL | let _ = if let Some(x) = arg { | _____________^ @@ -121,7 +121,7 @@ LL | | }; | |_____^ help: try: `arg.map_or_else(side_effect, |x| x)` error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:75:13 + --> tests/ui/option_if_let_else.rs:76:13 | LL | let _ = if let Some(x) = arg { | _____________^ @@ -144,7 +144,7 @@ LL ~ }, |x| x * x * x * x); | error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:108:13 + --> tests/ui/option_if_let_else.rs:109:13 | LL | / if let Some(idx) = s.find('.') { LL | | vec![s[..idx].to_string(), s[idx..].to_string()] @@ -154,7 +154,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 - --> tests/ui/option_if_let_else.rs:119:5 + --> tests/ui/option_if_let_else.rs:120:5 | LL | / if let Ok(binding) = variable { LL | | println!("Ok {binding}"); @@ -177,13 +177,13 @@ LL + }) | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:143:13 + --> tests/ui/option_if_let_else.rs:144: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 - --> tests/ui/option_if_let_else.rs:153:13 + --> tests/ui/option_if_let_else.rs:154:13 | LL | let _ = if let Some(x) = Some(0) { | _____________^ @@ -205,13 +205,13 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:181:13 + --> tests/ui/option_if_let_else.rs:182: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 - --> tests/ui/option_if_let_else.rs:185:13 + --> tests/ui/option_if_let_else.rs:186:13 | LL | let _ = if let Some(x) = Some(0) { | _____________^ @@ -231,7 +231,7 @@ LL ~ }); | error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:224:13 + --> tests/ui/option_if_let_else.rs:225:13 | LL | let _ = match s { | _____________^ @@ -241,7 +241,7 @@ LL | | }; | |_____^ help: try: `s.map_or(1, |string| string.len())` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:228:13 + --> tests/ui/option_if_let_else.rs:229:13 | LL | let _ = match Some(10) { | _____________^ @@ -251,7 +251,7 @@ LL | | }; | |_____^ help: try: `Some(10).map_or(5, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:234:13 + --> tests/ui/option_if_let_else.rs:235:13 | LL | let _ = match res { | _____________^ @@ -261,7 +261,7 @@ LL | | }; | |_____^ help: try: `res.map_or(1, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:238:13 + --> tests/ui/option_if_let_else.rs:239:13 | LL | let _ = match res { | _____________^ @@ -271,13 +271,13 @@ LL | | }; | |_____^ help: try: `res.map_or(1, |a| a + 1)` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:242:13 + --> tests/ui/option_if_let_else.rs:243: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 - --> tests/ui/option_if_let_else.rs:259:17 + --> tests/ui/option_if_let_else.rs:260:17 | LL | let _ = match initial { | _________________^ @@ -287,7 +287,7 @@ LL | | }; | |_________^ help: try: `initial.as_ref().map_or(42, |value| do_something(value))` error: use Option::map_or instead of an if let/else - --> tests/ui/option_if_let_else.rs:266:17 + --> tests/ui/option_if_let_else.rs:267:17 | LL | let _ = match initial { | _________________^ @@ -297,7 +297,7 @@ LL | | }; | |_________^ help: try: `initial.as_mut().map_or(42, |value| do_something2(value))` error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:289:24 + --> tests/ui/option_if_let_else.rs:290:24 | LL | let mut _hashmap = if let Some(hm) = &opt { | ________________________^ @@ -308,7 +308,7 @@ LL | | }; | |_____^ help: try: `opt.as_ref().map_or_else(HashMap::new, |hm| hm.clone())` error: use Option::map_or_else instead of an if let/else - --> tests/ui/option_if_let_else.rs:295:19 + --> tests/ui/option_if_let_else.rs:296:19 | LL | let mut _hm = if let Some(hm) = &opt { hm.clone() } else { new_map!() }; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `opt.as_ref().map_or_else(|| new_map!(), |hm| hm.clone())` 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