Skip to content

Commit 0db9959

Browse files
committed
clean-up
- move `is_allowed_vec_method` to utils -- shared between `useless_vec` and `ptr/` - add another test for non-standard macro brace case - rm unneeded `allow`s - rm duplicated tests - add comments to some tests
1 parent db54d19 commit 0db9959

File tree

8 files changed

+82
-101
lines changed

8 files changed

+82
-101
lines changed

clippy_lints/src/ptr.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::res::{MaybeDef, MaybeResPath};
33
use clippy_utils::source::SpanRangeExt;
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::visitors::contains_unsafe_block;
6-
use clippy_utils::{get_expr_use_or_unification_node, is_lint_allowed, std_or_core, sym};
6+
use clippy_utils::{get_expr_use_or_unification_node, is_allowed_vec_method, is_lint_allowed, std_or_core, sym};
77
use hir::LifetimeKind;
88
use rustc_abi::ExternAbi;
99
use rustc_errors::{Applicability, MultiSpan};
@@ -25,8 +25,6 @@ use rustc_trait_selection::infer::InferCtxtExt as _;
2525
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
2626
use std::{fmt, iter};
2727

28-
use crate::useless_vec::is_allowed_vec_method;
29-
3028
declare_clippy_lint! {
3129
/// ### What it does
3230
/// This lint checks for function arguments of type `&String`, `&Vec`,

clippy_lints/src/useless_vec.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use clippy_utils::msrvs::{self, Msrv};
1010
use clippy_utils::source::SpanRangeExt;
1111
use clippy_utils::ty::is_copy;
1212
use clippy_utils::visitors::for_each_local_use_after_expr;
13-
use clippy_utils::{get_parent_expr, higher, is_in_test, span_contains_comment, sym};
13+
use clippy_utils::{get_parent_expr, higher, is_allowed_vec_method, is_in_test, span_contains_comment};
1414
use rustc_errors::Applicability;
1515
use rustc_hir::{BorrowKind, Expr, ExprKind, HirId, LetStmt, Mutability, Node, Pat, PatKind};
1616
use rustc_lint::{LateContext, LateLintPass};
@@ -144,8 +144,9 @@ impl UselessVec {
144144
VecToArray::Impossible
145145
},
146146
// search for `for _ in vec![...]`
147-
Node::Expr(Expr { span, .. })
148-
if span.is_desugaring(DesugaringKind::ForLoop) && self.msrv.meets(cx, msrvs::ARRAY_INTO_ITERATOR) =>
147+
Node::Expr(expr)
148+
if expr.span.is_desugaring(DesugaringKind::ForLoop)
149+
&& self.msrv.meets(cx, msrvs::ARRAY_INTO_ITERATOR) =>
149150
{
150151
VecToArray::Possible
151152
},
@@ -276,9 +277,8 @@ impl SuggestedType {
276277
assert!(args_span.is_none_or(|s| !s.from_expansion()));
277278
assert!(len_span.is_none_or(|s| !s.from_expansion()));
278279

279-
let maybe_args = args_span
280-
.map(|sp| sp.get_source_text(cx).expect("spans are always crate-local"))
281-
.map_or(String::new(), |x| x.to_owned());
280+
let maybe_args = args_span.map(|sp| sp.get_source_text(cx).expect("spans are always crate-local"));
281+
let maybe_args = maybe_args.as_ref().map(|s| s.as_str()).unwrap_or_default();
282282
let maybe_len = len_span
283283
.map(|sp| sp.get_source_text(cx).expect("spans are always crate-local"))
284284
.map(|st| format!("; {st}"))
@@ -301,17 +301,6 @@ fn adjusts_to_slice(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
301301
matches!(cx.typeck_results().expr_ty_adjusted(e).kind(), ty::Ref(_, ty, _) if ty.is_slice())
302302
}
303303

304-
/// Checks if the given expression is a method call to a `Vec` method
305-
/// that also exists on slices. If this returns true, it means that
306-
/// this expression does not actually require a `Vec` and could just work with an array.
307-
pub fn is_allowed_vec_method(e: &Expr<'_>) -> bool {
308-
if let ExprKind::MethodCall(path, _, [], _) = e.kind {
309-
matches!(path.ident.name, sym::as_ptr | sym::is_empty | sym::len)
310-
} else {
311-
false
312-
}
313-
}
314-
315304
fn suggest_type(expr: &Expr<'_>) -> SuggestedType {
316305
if let ExprKind::AddrOf(BorrowKind::Ref, mutability, _) = expr.kind {
317306
// `expr` is `&vec![_]`, so suggest `&[_]` (or `&mut[_]` resp.)

clippy_utils/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3524,3 +3524,14 @@ pub fn is_expr_async_block(expr: &Expr<'_>) -> bool {
35243524
pub fn can_use_if_let_chains(cx: &LateContext<'_>, msrv: Msrv) -> bool {
35253525
cx.tcx.sess.edition().at_least_rust_2024() && msrv.meets(cx, msrvs::LET_CHAINS)
35263526
}
3527+
3528+
/// Checks if the given expression is a method call to a `Vec` method
3529+
/// that also exists on slices. If this returns true, it means that
3530+
/// this expression does not actually require a `Vec` and could just work with an array.
3531+
pub fn is_allowed_vec_method(e: &Expr<'_>) -> bool {
3532+
if let ExprKind::MethodCall(path, _, [], _) = e.kind {
3533+
matches!(path.ident.name, sym::as_ptr | sym::is_empty | sym::len)
3534+
} else {
3535+
false
3536+
}
3537+
}

tests/ui/useless_vec.fixed

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![warn(clippy::useless_vec)]
2-
#![allow(clippy::nonstandard_macro_braces, clippy::uninlined_format_args, unused)]
32

43
use std::rc::Rc;
54

@@ -39,17 +38,14 @@ fn main() {
3938
on_mut_slice(&mut [1, 2]);
4039
//~^ useless_vec
4140

42-
on_slice(&[1, 2]);
43-
//~^ useless_vec
44-
on_slice(&[1, 2]);
45-
on_mut_slice(&mut [1, 2]);
46-
//~^ useless_vec
4741
#[rustfmt::skip]
48-
on_slice(&[1, 2]);
49-
//~^ useless_vec
50-
on_slice(&[1, 2]);
51-
on_mut_slice(&mut [1, 2]);
52-
//~^ useless_vec
42+
#[allow(clippy::nonstandard_macro_braces)] // not an `expect` as it will only lint _before_ the fix
43+
{
44+
on_slice(&[1, 2]);
45+
//~^ useless_vec
46+
on_mut_slice(&mut [1, 2]);
47+
//~^ useless_vec
48+
};
5349

5450
on_slice(&[1; 2]);
5551
//~^ useless_vec
@@ -75,22 +71,24 @@ fn main() {
7571
on_vec(&vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
7672
on_mut_vec(&mut vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
7773

78-
// Ok
74+
// Ok, size of `vec` higher than `too_large_for_stack`
7975
for a in vec![1; 201] {
80-
println!("{:?}", a);
76+
println!("{a:?}");
8177
}
8278

8379
// https://github.com/rust-lang/rust-clippy/issues/2262#issuecomment-783979246
8480
let _x: i32 = [1, 2, 3].iter().sum();
8581
//~^ useless_vec
8682

87-
// Do lint
88-
let mut x = [1, 2, 3];
89-
//~^ useless_vec
90-
x.fill(123);
91-
dbg!(x[0]);
92-
dbg!(x.len());
93-
dbg!(x.iter().sum::<i32>());
83+
// Do lint, only used as slice
84+
{
85+
let mut x = [1, 2, 3];
86+
//~^ useless_vec
87+
x.fill(123);
88+
dbg!(x[0]);
89+
dbg!(x.len());
90+
dbg!(x.iter().sum::<i32>());
91+
}
9492

9593
let _x: &[i32] = &[1, 2, 3];
9694
//~^ useless_vec

tests/ui/useless_vec.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![warn(clippy::useless_vec)]
2-
#![allow(clippy::nonstandard_macro_braces, clippy::uninlined_format_args, unused)]
32

43
use std::rc::Rc;
54

@@ -39,17 +38,14 @@ fn main() {
3938
on_mut_slice(&mut vec![1, 2]);
4039
//~^ useless_vec
4140

42-
on_slice(&vec![1, 2]);
43-
//~^ useless_vec
44-
on_slice(&[1, 2]);
45-
on_mut_slice(&mut vec![1, 2]);
46-
//~^ useless_vec
4741
#[rustfmt::skip]
48-
on_slice(&vec!(1, 2));
49-
//~^ useless_vec
50-
on_slice(&[1, 2]);
51-
on_mut_slice(&mut vec![1, 2]);
52-
//~^ useless_vec
42+
#[allow(clippy::nonstandard_macro_braces)] // not an `expect` as it will only lint _before_ the fix
43+
{
44+
on_slice(&vec!(1, 2));
45+
//~^ useless_vec
46+
on_mut_slice(&mut vec!(1, 2));
47+
//~^ useless_vec
48+
};
5349

5450
on_slice(&vec![1; 2]);
5551
//~^ useless_vec
@@ -75,22 +71,24 @@ fn main() {
7571
on_vec(&vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
7672
on_mut_vec(&mut vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
7773

78-
// Ok
74+
// Ok, size of `vec` higher than `too_large_for_stack`
7975
for a in vec![1; 201] {
80-
println!("{:?}", a);
76+
println!("{a:?}");
8177
}
8278

8379
// https://github.com/rust-lang/rust-clippy/issues/2262#issuecomment-783979246
8480
let _x: i32 = vec![1, 2, 3].iter().sum();
8581
//~^ useless_vec
8682

87-
// Do lint
88-
let mut x = vec![1, 2, 3];
89-
//~^ useless_vec
90-
x.fill(123);
91-
dbg!(x[0]);
92-
dbg!(x.len());
93-
dbg!(x.iter().sum::<i32>());
83+
// Do lint, only used as slice
84+
{
85+
let mut x = vec![1, 2, 3];
86+
//~^ useless_vec
87+
x.fill(123);
88+
dbg!(x[0]);
89+
dbg!(x.len());
90+
dbg!(x.iter().sum::<i32>());
91+
}
9492

9593
let _x: &[i32] = &vec![1, 2, 3];
9694
//~^ useless_vec

tests/ui/useless_vec.stderr

Lines changed: 27 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: useless use of `vec!`
2-
--> tests/ui/useless_vec.rs:30:14
2+
--> tests/ui/useless_vec.rs:29:14
33
|
44
LL | on_slice(&vec![]);
55
| ^^^^^^^ help: you can use a slice directly: `&[]`
@@ -8,130 +8,118 @@ LL | on_slice(&vec![]);
88
= help: to override `-D warnings` add `#[allow(clippy::useless_vec)]`
99

1010
error: useless use of `vec!`
11-
--> tests/ui/useless_vec.rs:33:18
11+
--> tests/ui/useless_vec.rs:32:18
1212
|
1313
LL | on_mut_slice(&mut vec![]);
1414
| ^^^^^^^^^^^ help: you can use a slice directly: `&mut []`
1515

1616
error: useless use of `vec!`
17-
--> tests/ui/useless_vec.rs:36:14
17+
--> tests/ui/useless_vec.rs:35:14
1818
|
1919
LL | on_slice(&vec![1, 2]);
2020
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
2121

2222
error: useless use of `vec!`
23-
--> tests/ui/useless_vec.rs:39:18
23+
--> tests/ui/useless_vec.rs:38:18
2424
|
2525
LL | on_mut_slice(&mut vec![1, 2]);
2626
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1, 2]`
2727

2828
error: useless use of `vec!`
29-
--> tests/ui/useless_vec.rs:42:14
29+
--> tests/ui/useless_vec.rs:44:18
3030
|
31-
LL | on_slice(&vec![1, 2]);
32-
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
33-
34-
error: useless use of `vec!`
35-
--> tests/ui/useless_vec.rs:45:18
36-
|
37-
LL | on_mut_slice(&mut vec![1, 2]);
38-
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1, 2]`
31+
LL | on_slice(&vec!(1, 2));
32+
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
3933

4034
error: useless use of `vec!`
41-
--> tests/ui/useless_vec.rs:48:14
35+
--> tests/ui/useless_vec.rs:46:22
4236
|
43-
LL | on_slice(&vec!(1, 2));
44-
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
45-
46-
error: useless use of `vec!`
47-
--> tests/ui/useless_vec.rs:51:18
48-
|
49-
LL | on_mut_slice(&mut vec![1, 2]);
50-
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1, 2]`
37+
LL | on_mut_slice(&mut vec!(1, 2));
38+
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1, 2]`
5139

5240
error: useless use of `vec!`
53-
--> tests/ui/useless_vec.rs:54:14
41+
--> tests/ui/useless_vec.rs:50:14
5442
|
5543
LL | on_slice(&vec![1; 2]);
5644
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1; 2]`
5745

5846
error: useless use of `vec!`
59-
--> tests/ui/useless_vec.rs:57:18
47+
--> tests/ui/useless_vec.rs:53:18
6048
|
6149
LL | on_mut_slice(&mut vec![1; 2]);
6250
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1; 2]`
6351

6452
error: useless use of `vec!`
65-
--> tests/ui/useless_vec.rs:84:19
53+
--> tests/ui/useless_vec.rs:80:19
6654
|
6755
LL | let _x: i32 = vec![1, 2, 3].iter().sum();
6856
| ^^^^^^^^^^^^^ help: you can use an array directly: `[1, 2, 3]`
6957

7058
error: useless use of `vec!`
71-
--> tests/ui/useless_vec.rs:88:17
59+
--> tests/ui/useless_vec.rs:85:21
7260
|
73-
LL | let mut x = vec![1, 2, 3];
74-
| ^^^^^^^^^^^^^ help: you can use an array directly: `[1, 2, 3]`
61+
LL | let mut x = vec![1, 2, 3];
62+
| ^^^^^^^^^^^^^ help: you can use an array directly: `[1, 2, 3]`
7563

7664
error: useless use of `vec!`
77-
--> tests/ui/useless_vec.rs:95:22
65+
--> tests/ui/useless_vec.rs:93:22
7866
|
7967
LL | let _x: &[i32] = &vec![1, 2, 3];
8068
| ^^^^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2, 3]`
8169

8270
error: useless use of `vec!`
83-
--> tests/ui/useless_vec.rs:98:14
71+
--> tests/ui/useless_vec.rs:96:14
8472
|
8573
LL | for _ in vec![1, 2, 3] {}
8674
| ^^^^^^^^^^^^^ help: you can use an array directly: `[1, 2, 3]`
8775

8876
error: useless use of `vec!`
89-
--> tests/ui/useless_vec.rs:138:20
77+
--> tests/ui/useless_vec.rs:136:20
9078
|
9179
LL | for _string in vec![repro!(true), repro!(null)] {
9280
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[repro!(true), repro!(null)]`
9381

9482
error: useless use of `vec!`
95-
--> tests/ui/useless_vec.rs:156:18
83+
--> tests/ui/useless_vec.rs:154:18
9684
|
9785
LL | in_macro!(1, vec![1, 2], vec![1; 2]);
9886
| ^^^^^^^^^^ help: you can use an array directly: `[1, 2]`
9987

10088
error: useless use of `vec!`
101-
--> tests/ui/useless_vec.rs:156:30
89+
--> tests/ui/useless_vec.rs:154:30
10290
|
10391
LL | in_macro!(1, vec![1, 2], vec![1; 2]);
10492
| ^^^^^^^^^^ help: you can use an array directly: `[1; 2]`
10593

10694
error: useless use of `vec!`
107-
--> tests/ui/useless_vec.rs:177:14
95+
--> tests/ui/useless_vec.rs:175:14
10896
|
10997
LL | for a in vec![1, 2, 3] {
11098
| ^^^^^^^^^^^^^ help: you can use an array directly: `[1, 2, 3]`
11199

112100
error: useless use of `vec!`
113-
--> tests/ui/useless_vec.rs:182:14
101+
--> tests/ui/useless_vec.rs:180:14
114102
|
115103
LL | for a in vec![String::new(), String::new()] {
116104
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: you can use an array directly: `[String::new(), String::new()]`
117105

118106
error: useless use of `vec!`
119-
--> tests/ui/useless_vec.rs:215:33
107+
--> tests/ui/useless_vec.rs:213:33
120108
|
121109
LL | this_macro_doesnt_need_vec!(vec![1]);
122110
| ^^^^^^^ help: you can use an array directly: `[1]`
123111

124112
error: useless use of `vec!`
125-
--> tests/ui/useless_vec.rs:242:14
113+
--> tests/ui/useless_vec.rs:240:14
126114
|
127115
LL | for a in &(vec![1, 2]) {}
128116
| ^^^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
129117

130118
error: useless use of `vec!`
131-
--> tests/ui/useless_vec.rs:250:13
119+
--> tests/ui/useless_vec.rs:248:13
132120
|
133121
LL | let v = &vec![];
134122
| ^^^^^^^ help: you can use a slice directly: `&[]`
135123

136-
error: aborting due to 22 previous errors
124+
error: aborting due to 20 previous errors
137125

tests/ui/useless_vec_unfixable.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@no-rustfix: no suggestions
2-
32
#![warn(clippy::useless_vec)]
43

54
// Regression test for <https://github.com/rust-lang/rust-clippy/issues/13692>.

tests/ui/useless_vec_unfixable.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: useless use of `vec!`
2-
--> tests/ui/useless_vec_unfixable.rs:8:26
2+
--> tests/ui/useless_vec_unfixable.rs:7:26
33
|
44
LL | let _some_variable = vec![
55
| __________________________^

0 commit comments

Comments
 (0)