diff options
| author | bors <bors@rust-lang.org> | 2023-12-12 22:57:24 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-12-12 22:57:24 +0000 |
| commit | c19508b356c67eb188504fc41692220cc760508e (patch) | |
| tree | 0b8f51ea28ea64edbb500bea282f7d6e07091df3 /tests | |
| parent | 2e96c74dcea20816a34a73d2d3e336a4ebd35eef (diff) | |
| parent | 884bec3d8558aef4c57bae1244fca76ab668e5b6 (diff) | |
Auto merge of #11895 - ericwu2003:useless_vec-FP, r=blyxyas
Useless vec false positive changelog: [`useless_vec`]: fix false positive in macros. fixes #11861 We delay the emission of `useless_vec` lints to the check_crate_post stage, which allows us to effectively undo lints if we find that a `vec![]` expression is being used multiple times after macro expansion.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/vec.fixed | 34 | ||||
| -rw-r--r-- | tests/ui/vec.rs | 34 | ||||
| -rw-r--r-- | tests/ui/vec.stderr | 8 |
3 files changed, 75 insertions, 1 deletions
diff --git a/tests/ui/vec.fixed b/tests/ui/vec.fixed index bcbca971a78..81b8bd7da77 100644 --- a/tests/ui/vec.fixed +++ b/tests/ui/vec.fixed @@ -176,3 +176,37 @@ fn below() { let _: String = a; } } + +fn func_needing_vec(_bar: usize, _baz: Vec<usize>) {} +fn func_not_needing_vec(_bar: usize, _baz: usize) {} + +fn issue11861() { + macro_rules! this_macro_needs_vec { + ($x:expr) => {{ + func_needing_vec($x.iter().sum(), $x); + for _ in $x {} + }}; + } + macro_rules! this_macro_doesnt_need_vec { + ($x:expr) => {{ func_not_needing_vec($x.iter().sum(), $x.iter().sum()) }}; + } + + // Do not lint the next line + this_macro_needs_vec!(vec![1]); + this_macro_doesnt_need_vec!([1]); //~ ERROR: useless use of `vec!` + + macro_rules! m { + ($x:expr) => { + fn f2() { + let _x: Vec<i32> = $x; + } + fn f() { + let _x = $x; + $x.starts_with(&[]); + } + }; + } + + // should not lint + m!(vec![1]); +} diff --git a/tests/ui/vec.rs b/tests/ui/vec.rs index 087425585de..5aca9b2925c 100644 --- a/tests/ui/vec.rs +++ b/tests/ui/vec.rs @@ -176,3 +176,37 @@ fn below() { let _: String = a; } } + +fn func_needing_vec(_bar: usize, _baz: Vec<usize>) {} +fn func_not_needing_vec(_bar: usize, _baz: usize) {} + +fn issue11861() { + macro_rules! this_macro_needs_vec { + ($x:expr) => {{ + func_needing_vec($x.iter().sum(), $x); + for _ in $x {} + }}; + } + macro_rules! this_macro_doesnt_need_vec { + ($x:expr) => {{ func_not_needing_vec($x.iter().sum(), $x.iter().sum()) }}; + } + + // Do not lint the next line + this_macro_needs_vec!(vec![1]); + this_macro_doesnt_need_vec!(vec![1]); //~ ERROR: useless use of `vec!` + + macro_rules! m { + ($x:expr) => { + fn f2() { + let _x: Vec<i32> = $x; + } + fn f() { + let _x = $x; + $x.starts_with(&[]); + } + }; + } + + // should not lint + m!(vec![1]); +} diff --git a/tests/ui/vec.stderr b/tests/ui/vec.stderr index fc261838fe3..c9018f94f9d 100644 --- a/tests/ui/vec.stderr +++ b/tests/ui/vec.stderr @@ -115,5 +115,11 @@ error: useless use of `vec!` LL | for a in vec![String::new(), String::new()] { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[String::new(), String::new()]` -error: aborting due to 19 previous errors +error: useless use of `vec!` + --> $DIR/vec.rs:196:33 + | +LL | this_macro_doesnt_need_vec!(vec![1]); + | ^^^^^^^ help: you can use an array directly: `[1]` + +error: aborting due to 20 previous errors |
