From 4547b622d8d29df964fa2914213088b148c498fc Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:32 +0200 Subject: Merging upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- .../allow_mixed_uninlined_format_args/clippy.toml | 1 + .../uninlined_format_args.fixed | 14 ++++ .../uninlined_format_args.rs | 14 ++++ .../uninlined_format_args.stderr | 76 ++++++++++++++++++++++ .../arithmetic_side_effects_allowed.rs | 11 +++- .../await_holding_invalid_type.stderr | 4 +- .../tests/ui-toml/expect_used/expect_used.rs | 23 ++++--- .../tests/ui-toml/expect_used/expect_used.stderr | 4 +- src/tools/clippy/tests/ui-toml/mut_key/clippy.toml | 1 + src/tools/clippy/tests/ui-toml/mut_key/mut_key.rs | 53 +++++++++++++++ .../clippy/tests/ui-toml/print_macro/clippy.toml | 1 + .../tests/ui-toml/print_macro/print_macro.rs | 20 ++++++ .../tests/ui-toml/print_macro/print_macro.stderr | 18 +++++ .../ui-toml/toml_disallowed_methods/clippy.toml | 6 ++ .../conf_disallowed_methods.rs | 30 +++++++++ .../conf_disallowed_methods.stderr | 50 +++++++++++--- .../toml_unknown_key/conf_unknown_key.stderr | 4 ++ .../tests/ui-toml/unwrap_used/unwrap_used.rs | 21 ++++-- .../tests/ui-toml/unwrap_used/unwrap_used.stderr | 32 ++++----- .../clippy/tests/ui-toml/vec_box_sized/test.rs | 5 +- .../clippy/tests/ui-toml/vec_box_sized/test.stderr | 6 +- 21 files changed, 345 insertions(+), 49 deletions(-) create mode 100644 src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml create mode 100644 src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed create mode 100644 src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs create mode 100644 src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr create mode 100644 src/tools/clippy/tests/ui-toml/mut_key/clippy.toml create mode 100644 src/tools/clippy/tests/ui-toml/mut_key/mut_key.rs create mode 100644 src/tools/clippy/tests/ui-toml/print_macro/clippy.toml create mode 100644 src/tools/clippy/tests/ui-toml/print_macro/print_macro.rs create mode 100644 src/tools/clippy/tests/ui-toml/print_macro/print_macro.stderr (limited to 'src/tools/clippy/tests/ui-toml') diff --git a/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml b/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml new file mode 100644 index 000000000..b95e806aa --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/clippy.toml @@ -0,0 +1 @@ +allow-mixed-uninlined-format-args = false diff --git a/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed b/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed new file mode 100644 index 000000000..aa8b45b5f --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.fixed @@ -0,0 +1,14 @@ +// run-rustfix +#![warn(clippy::uninlined_format_args)] + +fn main() { + let local_i32 = 1; + let local_f64 = 2.0; + let local_opt: Option = Some(3); + + println!("val='{local_i32}'"); + println!("Hello x is {local_f64:.local_i32$}"); + println!("Hello {local_i32} is {local_f64:.*}", 5); + println!("Hello {local_i32} is {local_f64:.*}", 5); + println!("{local_i32}, {}", local_opt.unwrap()); +} diff --git a/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs b/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs new file mode 100644 index 000000000..ad2e4863e --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.rs @@ -0,0 +1,14 @@ +// run-rustfix +#![warn(clippy::uninlined_format_args)] + +fn main() { + let local_i32 = 1; + let local_f64 = 2.0; + let local_opt: Option = Some(3); + + println!("val='{}'", local_i32); + println!("Hello {} is {:.*}", "x", local_i32, local_f64); + println!("Hello {} is {:.*}", local_i32, 5, local_f64); + println!("Hello {} is {2:.*}", local_i32, 5, local_f64); + println!("{}, {}", local_i32, local_opt.unwrap()); +} diff --git a/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr b/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr new file mode 100644 index 000000000..ee9417621 --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/allow_mixed_uninlined_format_args/uninlined_format_args.stderr @@ -0,0 +1,76 @@ +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:9:5 + | +LL | println!("val='{}'", local_i32); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D clippy::uninlined-format-args` implied by `-D warnings` +help: change this to + | +LL - println!("val='{}'", local_i32); +LL + println!("val='{local_i32}'"); + | + +error: literal with an empty format string + --> $DIR/uninlined_format_args.rs:10:35 + | +LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64); + | ^^^ + | + = note: `-D clippy::print-literal` implied by `-D warnings` +help: try this + | +LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64); +LL + println!("Hello x is {:.*}", local_i32, local_f64); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:10:5 + | +LL | println!("Hello {} is {:.*}", "x", local_i32, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("Hello {} is {:.*}", "x", local_i32, local_f64); +LL + println!("Hello {} is {local_f64:.local_i32$}", "x"); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:11:5 + | +LL | println!("Hello {} is {:.*}", local_i32, 5, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("Hello {} is {:.*}", local_i32, 5, local_f64); +LL + println!("Hello {local_i32} is {local_f64:.*}", 5); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:12:5 + | +LL | println!("Hello {} is {2:.*}", local_i32, 5, local_f64); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("Hello {} is {2:.*}", local_i32, 5, local_f64); +LL + println!("Hello {local_i32} is {local_f64:.*}", 5); + | + +error: variables can be used directly in the `format!` string + --> $DIR/uninlined_format_args.rs:13:5 + | +LL | println!("{}, {}", local_i32, local_opt.unwrap()); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | +help: change this to + | +LL - println!("{}, {}", local_i32, local_opt.unwrap()); +LL + println!("{local_i32}, {}", local_opt.unwrap()); + | + +error: aborting due to 6 previous errors + diff --git a/src/tools/clippy/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs b/src/tools/clippy/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs index 1aed09b7c..e8a023ab1 100644 --- a/src/tools/clippy/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs +++ b/src/tools/clippy/tests/ui-toml/arithmetic_side_effects_allowed/arithmetic_side_effects_allowed.rs @@ -1,6 +1,6 @@ #![warn(clippy::arithmetic_side_effects)] -use core::ops::Add; +use core::ops::{Add, Neg}; #[derive(Clone, Copy)] struct Point { @@ -16,9 +16,18 @@ impl Add for Point { } } +impl Neg for Point { + type Output = Self; + + fn neg(self) -> Self::Output { + todo!() + } +} + fn main() { let _ = Point { x: 1, y: 0 } + Point { x: 2, y: 3 }; let point: Point = Point { x: 1, y: 0 }; let _ = point + point; + let _ = -point; } diff --git a/src/tools/clippy/tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.stderr b/src/tools/clippy/tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.stderr index 4c7599843..825aa1487 100644 --- a/src/tools/clippy/tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.stderr +++ b/src/tools/clippy/tests/ui-toml/await_holding_invalid_type/await_holding_invalid_type.stderr @@ -4,7 +4,7 @@ error: `std::string::String` may not be held across an `await` point per `clippy LL | let _x = String::from("hello"); | ^^ | - = note: strings are bad + = note: strings are bad (from clippy.toml) = note: `-D clippy::await-holding-invalid-type` implied by `-D warnings` error: `std::net::Ipv4Addr` may not be held across an `await` point per `clippy.toml` @@ -19,7 +19,7 @@ error: `std::string::String` may not be held across an `await` point per `clippy LL | let _x = String::from("hi!"); | ^^ | - = note: strings are bad + = note: strings are bad (from clippy.toml) error: aborting due to 3 previous errors diff --git a/src/tools/clippy/tests/ui-toml/expect_used/expect_used.rs b/src/tools/clippy/tests/ui-toml/expect_used/expect_used.rs index 22dcd3ae9..bff97d97d 100644 --- a/src/tools/clippy/tests/ui-toml/expect_used/expect_used.rs +++ b/src/tools/clippy/tests/ui-toml/expect_used/expect_used.rs @@ -16,14 +16,19 @@ fn main() { expect_result(); } -#[test] -fn test_expect_option() { - let opt = Some(0); - let _ = opt.expect(""); -} +#[cfg(test)] +mod issue9612 { + // should not lint in `#[cfg(test)]` modules + #[test] + fn test_fn() { + let _a: u8 = 2.try_into().unwrap(); + let _a: u8 = 3.try_into().expect(""); -#[test] -fn test_expect_result() { - let res: Result = Ok(0); - let _ = res.expect(""); + util(); + } + + fn util() { + let _a: u8 = 4.try_into().unwrap(); + let _a: u8 = 5.try_into().expect(""); + } } diff --git a/src/tools/clippy/tests/ui-toml/expect_used/expect_used.stderr b/src/tools/clippy/tests/ui-toml/expect_used/expect_used.stderr index 28a08599c..1e9bb48c3 100644 --- a/src/tools/clippy/tests/ui-toml/expect_used/expect_used.stderr +++ b/src/tools/clippy/tests/ui-toml/expect_used/expect_used.stderr @@ -1,4 +1,4 @@ -error: used `expect()` on `an Option` value +error: used `expect()` on an `Option` value --> $DIR/expect_used.rs:6:13 | LL | let _ = opt.expect(""); @@ -7,7 +7,7 @@ LL | let _ = opt.expect(""); = help: if this value is `None`, it will panic = note: `-D clippy::expect-used` implied by `-D warnings` -error: used `expect()` on `a Result` value +error: used `expect()` on a `Result` value --> $DIR/expect_used.rs:11:13 | LL | let _ = res.expect(""); diff --git a/src/tools/clippy/tests/ui-toml/mut_key/clippy.toml b/src/tools/clippy/tests/ui-toml/mut_key/clippy.toml new file mode 100644 index 000000000..6d33e192e --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/mut_key/clippy.toml @@ -0,0 +1 @@ +ignore-interior-mutability = ["mut_key::Counted"] \ No newline at end of file diff --git a/src/tools/clippy/tests/ui-toml/mut_key/mut_key.rs b/src/tools/clippy/tests/ui-toml/mut_key/mut_key.rs new file mode 100644 index 000000000..667c51cb4 --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/mut_key/mut_key.rs @@ -0,0 +1,53 @@ +// compile-flags: --crate-name mut_key + +#![warn(clippy::mutable_key_type)] + +use std::cmp::{Eq, PartialEq}; +use std::collections::{HashMap, HashSet}; +use std::hash::{Hash, Hasher}; +use std::ops::Deref; +use std::sync::atomic::{AtomicUsize, Ordering}; + +struct Counted { + count: AtomicUsize, + val: T, +} + +impl Clone for Counted { + fn clone(&self) -> Self { + Self { + count: AtomicUsize::new(0), + val: self.val.clone(), + } + } +} + +impl PartialEq for Counted { + fn eq(&self, other: &Self) -> bool { + self.val == other.val + } +} +impl Eq for Counted {} + +impl Hash for Counted { + fn hash(&self, state: &mut H) { + self.val.hash(state); + } +} + +impl Deref for Counted { + type Target = T; + + fn deref(&self) -> &T { + self.count.fetch_add(1, Ordering::AcqRel); + &self.val + } +} + +// This is not linted because `"mut_key::Counted"` is in +// `arc_like_types` in `clippy.toml` +fn should_not_take_this_arg(_v: HashSet>) {} + +fn main() { + should_not_take_this_arg(HashSet::new()); +} diff --git a/src/tools/clippy/tests/ui-toml/print_macro/clippy.toml b/src/tools/clippy/tests/ui-toml/print_macro/clippy.toml new file mode 100644 index 000000000..40b1dda5b --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/print_macro/clippy.toml @@ -0,0 +1 @@ +allow-print-in-tests = true diff --git a/src/tools/clippy/tests/ui-toml/print_macro/print_macro.rs b/src/tools/clippy/tests/ui-toml/print_macro/print_macro.rs new file mode 100644 index 000000000..5aefb6a6b --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/print_macro/print_macro.rs @@ -0,0 +1,20 @@ +// compile-flags: --test +#![warn(clippy::print_stdout)] +#![warn(clippy::print_stderr)] + +fn foo(n: u32) { + print!("{n}"); + eprint!("{n}"); +} + +#[test] +pub fn foo1() { + print!("{}", 1); + eprint!("{}", 1); +} + +#[cfg(test)] +fn foo3() { + print!("{}", 1); + eprint!("{}", 1); +} diff --git a/src/tools/clippy/tests/ui-toml/print_macro/print_macro.stderr b/src/tools/clippy/tests/ui-toml/print_macro/print_macro.stderr new file mode 100644 index 000000000..d4b1ae84f --- /dev/null +++ b/src/tools/clippy/tests/ui-toml/print_macro/print_macro.stderr @@ -0,0 +1,18 @@ +error: use of `print!` + --> $DIR/print_macro.rs:6:5 + | +LL | print!("{n}"); + | ^^^^^^^^^^^^^ + | + = note: `-D clippy::print-stdout` implied by `-D warnings` + +error: use of `eprint!` + --> $DIR/print_macro.rs:7:5 + | +LL | eprint!("{n}"); + | ^^^^^^^^^^^^^^ + | + = note: `-D clippy::print-stderr` implied by `-D warnings` + +error: aborting due to 2 previous errors + diff --git a/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/clippy.toml b/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/clippy.toml index 28774db62..41dbd5068 100644 --- a/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/clippy.toml +++ b/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/clippy.toml @@ -8,4 +8,10 @@ disallowed-methods = [ { path = "regex::Regex::is_match", reason = "no matching allowed" }, # can use an inline table but omit reason { path = "regex::Regex::new" }, + # local paths + "conf_disallowed_methods::local_fn", + "conf_disallowed_methods::local_mod::f", + "conf_disallowed_methods::Struct::method", + "conf_disallowed_methods::Trait::provided_method", + "conf_disallowed_methods::Trait::implemented_method", ] diff --git a/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs b/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs index b483f1600..2f3160c83 100644 --- a/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs +++ b/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.rs @@ -1,3 +1,5 @@ +// compile-flags: --crate-name conf_disallowed_methods + #![warn(clippy::disallowed_methods)] extern crate futures; @@ -6,6 +8,27 @@ extern crate regex; use futures::stream::{empty, select_all}; use regex::Regex; +fn local_fn() {} + +struct Struct; + +impl Struct { + fn method(&self) {} +} + +trait Trait { + fn provided_method(&self) {} + fn implemented_method(&self); +} + +impl Trait for Struct { + fn implemented_method(&self) {} +} + +mod local_mod { + pub fn f() {} +} + fn main() { let re = Regex::new(r"ab.*c").unwrap(); re.is_match("abc"); @@ -26,4 +49,11 @@ fn main() { // resolve ambiguity between `futures::stream::select_all` the module and the function let same_name_as_module = select_all(vec![empty::<()>()]); + + local_fn(); + local_mod::f(); + let s = Struct; + s.method(); + s.provided_method(); + s.implemented_method(); } diff --git a/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr b/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr index 6d78c32e1..148d1cae5 100644 --- a/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr +++ b/src/tools/clippy/tests/ui-toml/toml_disallowed_methods/conf_disallowed_methods.stderr @@ -1,5 +1,5 @@ error: use of a disallowed method `regex::Regex::new` - --> $DIR/conf_disallowed_methods.rs:10:14 + --> $DIR/conf_disallowed_methods.rs:33:14 | LL | let re = Regex::new(r"ab.*c").unwrap(); | ^^^^^^^^^^^^^^^^^^^^ @@ -7,7 +7,7 @@ LL | let re = Regex::new(r"ab.*c").unwrap(); = note: `-D clippy::disallowed-methods` implied by `-D warnings` error: use of a disallowed method `regex::Regex::is_match` - --> $DIR/conf_disallowed_methods.rs:11:5 + --> $DIR/conf_disallowed_methods.rs:34:5 | LL | re.is_match("abc"); | ^^^^^^^^^^^^^^^^^^ @@ -15,46 +15,76 @@ LL | re.is_match("abc"); = note: no matching allowed (from clippy.toml) error: use of a disallowed method `std::iter::Iterator::sum` - --> $DIR/conf_disallowed_methods.rs:14:5 + --> $DIR/conf_disallowed_methods.rs:37:5 | LL | a.iter().sum::(); | ^^^^^^^^^^^^^^^^^^^^^ error: use of a disallowed method `slice::sort_unstable` - --> $DIR/conf_disallowed_methods.rs:16:5 + --> $DIR/conf_disallowed_methods.rs:39:5 | LL | a.sort_unstable(); | ^^^^^^^^^^^^^^^^^ error: use of a disallowed method `f32::clamp` - --> $DIR/conf_disallowed_methods.rs:18:13 + --> $DIR/conf_disallowed_methods.rs:41:13 | LL | let _ = 2.0f32.clamp(3.0f32, 4.0f32); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: use of a disallowed method `regex::Regex::new` - --> $DIR/conf_disallowed_methods.rs:21:61 + --> $DIR/conf_disallowed_methods.rs:44:61 | LL | let indirect: fn(&str) -> Result = Regex::new; | ^^^^^^^^^^ error: use of a disallowed method `f32::clamp` - --> $DIR/conf_disallowed_methods.rs:24:28 + --> $DIR/conf_disallowed_methods.rs:47:28 | LL | let in_call = Box::new(f32::clamp); | ^^^^^^^^^^ error: use of a disallowed method `regex::Regex::new` - --> $DIR/conf_disallowed_methods.rs:25:53 + --> $DIR/conf_disallowed_methods.rs:48:53 | LL | let in_method_call = ["^", "$"].into_iter().map(Regex::new); | ^^^^^^^^^^ error: use of a disallowed method `futures::stream::select_all` - --> $DIR/conf_disallowed_methods.rs:28:31 + --> $DIR/conf_disallowed_methods.rs:51:31 | LL | let same_name_as_module = select_all(vec![empty::<()>()]); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: aborting due to 9 previous errors +error: use of a disallowed method `conf_disallowed_methods::local_fn` + --> $DIR/conf_disallowed_methods.rs:53:5 + | +LL | local_fn(); + | ^^^^^^^^^^ + +error: use of a disallowed method `conf_disallowed_methods::local_mod::f` + --> $DIR/conf_disallowed_methods.rs:54:5 + | +LL | local_mod::f(); + | ^^^^^^^^^^^^^^ + +error: use of a disallowed method `conf_disallowed_methods::Struct::method` + --> $DIR/conf_disallowed_methods.rs:56:5 + | +LL | s.method(); + | ^^^^^^^^^^ + +error: use of a disallowed method `conf_disallowed_methods::Trait::provided_method` + --> $DIR/conf_disallowed_methods.rs:57:5 + | +LL | s.provided_method(); + | ^^^^^^^^^^^^^^^^^^^ + +error: use of a disallowed method `conf_disallowed_methods::Trait::implemented_method` + --> $DIR/conf_disallowed_methods.rs:58:5 + | +LL | s.implemented_method(); + | ^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 14 previous errors diff --git a/src/tools/clippy/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/src/tools/clippy/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index 82ee80541..01a5e962c 100644 --- a/src/tools/clippy/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/src/tools/clippy/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -1,6 +1,8 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of allow-dbg-in-tests allow-expect-in-tests + allow-mixed-uninlined-format-args + allow-print-in-tests allow-unwrap-in-tests allowed-scripts arithmetic-side-effects-allowed @@ -20,8 +22,10 @@ error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown fie enforced-import-renames enum-variant-name-threshold enum-variant-size-threshold + ignore-interior-mutability large-error-threshold literal-representation-threshold + matches-for-let-else max-fn-params-bools max-include-file-size max-struct-bools diff --git a/src/tools/clippy/tests/ui-toml/unwrap_used/unwrap_used.rs b/src/tools/clippy/tests/ui-toml/unwrap_used/unwrap_used.rs index 0e82fb20e..bc8e8c1f0 100644 --- a/src/tools/clippy/tests/ui-toml/unwrap_used/unwrap_used.rs +++ b/src/tools/clippy/tests/ui-toml/unwrap_used/unwrap_used.rs @@ -66,8 +66,21 @@ fn main() { } } -#[test] -fn test() { - let boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]); - let _ = boxed_slice.get(1).unwrap(); +#[cfg(test)] +mod issue9612 { + // should not lint in `#[cfg(test)]` modules + #[test] + fn test_fn() { + let _a: u8 = 2.try_into().unwrap(); + let _a: u8 = 3.try_into().expect(""); + + util(); + } + + fn util() { + let _a: u8 = 4.try_into().unwrap(); + let _a: u8 = 5.try_into().expect(""); + // should still warn + let _ = Box::new([0]).get(1).unwrap(); + } } diff --git a/src/tools/clippy/tests/ui-toml/unwrap_used/unwrap_used.stderr b/src/tools/clippy/tests/ui-toml/unwrap_used/unwrap_used.stderr index 681b5eaf5..94b5ef663 100644 --- a/src/tools/clippy/tests/ui-toml/unwrap_used/unwrap_used.stderr +++ b/src/tools/clippy/tests/ui-toml/unwrap_used/unwrap_used.stderr @@ -10,7 +10,7 @@ note: the lint level is defined here LL | #![deny(clippy::get_unwrap)] | ^^^^^^^^^^^^^^^^^^ -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:35:17 | LL | let _ = boxed_slice.get(1).unwrap(); @@ -25,7 +25,7 @@ error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more co LL | let _ = some_slice.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_slice[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:36:17 | LL | let _ = some_slice.get(0).unwrap(); @@ -39,7 +39,7 @@ error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more conc LL | let _ = some_vec.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vec[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:37:17 | LL | let _ = some_vec.get(0).unwrap(); @@ -53,7 +53,7 @@ error: called `.get().unwrap()` on a VecDeque. Using `[]` is more clear and more LL | let _ = some_vecdeque.get(0).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vecdeque[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:38:17 | LL | let _ = some_vecdeque.get(0).unwrap(); @@ -67,7 +67,7 @@ error: called `.get().unwrap()` on a HashMap. Using `[]` is more clear and more LL | let _ = some_hashmap.get(&1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_hashmap[&1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:39:17 | LL | let _ = some_hashmap.get(&1).unwrap(); @@ -81,7 +81,7 @@ error: called `.get().unwrap()` on a BTreeMap. Using `[]` is more clear and more LL | let _ = some_btreemap.get(&1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_btreemap[&1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:40:17 | LL | let _ = some_btreemap.get(&1).unwrap(); @@ -95,7 +95,7 @@ error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more co LL | let _: u8 = *boxed_slice.get(1).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `boxed_slice[1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:44:22 | LL | let _: u8 = *boxed_slice.get(1).unwrap(); @@ -109,7 +109,7 @@ error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and mor LL | *boxed_slice.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `boxed_slice[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:49:10 | LL | *boxed_slice.get_mut(0).unwrap() = 1; @@ -123,7 +123,7 @@ error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and mor LL | *some_slice.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_slice[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:50:10 | LL | *some_slice.get_mut(0).unwrap() = 1; @@ -137,7 +137,7 @@ error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more LL | *some_vec.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:51:10 | LL | *some_vec.get_mut(0).unwrap() = 1; @@ -151,7 +151,7 @@ error: called `.get_mut().unwrap()` on a VecDeque. Using `[]` is more clear and LL | *some_vecdeque.get_mut(0).unwrap() = 1; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vecdeque[0]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:52:10 | LL | *some_vecdeque.get_mut(0).unwrap() = 1; @@ -165,7 +165,7 @@ error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more conc LL | let _ = some_vec.get(0..1).unwrap().to_vec(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0..1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:64:17 | LL | let _ = some_vec.get(0..1).unwrap().to_vec(); @@ -179,7 +179,7 @@ error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `some_vec[0..1]` -error: used `unwrap()` on `an Option` value +error: used `unwrap()` on an `Option` value --> $DIR/unwrap_used.rs:65:17 | LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); @@ -188,10 +188,10 @@ LL | let _ = some_vec.get_mut(0..1).unwrap().to_vec(); = help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise - --> $DIR/unwrap_used.rs:72:13 + --> $DIR/unwrap_used.rs:84:17 | -LL | let _ = boxed_slice.get(1).unwrap(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&boxed_slice[1]` +LL | let _ = Box::new([0]).get(1).unwrap(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&Box::new([0])[1]` error: aborting due to 27 previous errors diff --git a/src/tools/clippy/tests/ui-toml/vec_box_sized/test.rs b/src/tools/clippy/tests/ui-toml/vec_box_sized/test.rs index bf04bee16..4c46deb58 100644 --- a/src/tools/clippy/tests/ui-toml/vec_box_sized/test.rs +++ b/src/tools/clippy/tests/ui-toml/vec_box_sized/test.rs @@ -7,8 +7,9 @@ struct C { } struct Foo(Vec>); -struct Bar(Vec>); -struct Baz(Vec>); +struct Bar(Vec>); +struct Quux(Vec>); +struct Baz(Vec>); struct BarBaz(Vec>); struct FooBarBaz(Vec>); diff --git a/src/tools/clippy/tests/ui-toml/vec_box_sized/test.stderr b/src/tools/clippy/tests/ui-toml/vec_box_sized/test.stderr index cf194de3c..55de68f8e 100644 --- a/src/tools/clippy/tests/ui-toml/vec_box_sized/test.stderr +++ b/src/tools/clippy/tests/ui-toml/vec_box_sized/test.stderr @@ -9,11 +9,11 @@ LL | struct Foo(Vec>); error: `Vec` is already on the heap, the boxing is unnecessary --> $DIR/test.rs:10:12 | -LL | struct Bar(Vec>); - | ^^^^^^^^^^^^^ help: try: `Vec` +LL | struct Bar(Vec>); + | ^^^^^^^^^^^^^ help: try: `Vec` error: `Vec` is already on the heap, the boxing is unnecessary - --> $DIR/test.rs:13:18 + --> $DIR/test.rs:14:18 | LL | struct FooBarBaz(Vec>); | ^^^^^^^^^^^ help: try: `Vec` -- cgit v1.2.3