Skip to content

Commit d2ba777

Browse files
committed
Auto merge of #7036 - horacimacias:master, r=giraffate
consider mutability on useless_vec suggestions fixes #7035 changelog: Now the suggested by `useless_vec` considers mutability to suggest either `&[]`, as before, or `&mut []` if the used reference is mutable.
2 parents 57406c9 + 8a50923 commit d2ba777

File tree

4 files changed

+111
-19
lines changed

4 files changed

+111
-19
lines changed

clippy_lints/src/vec.rs

+42-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clippy_utils::source::snippet_with_applicability;
66
use clippy_utils::ty::is_copy;
77
use if_chain::if_chain;
88
use rustc_errors::Applicability;
9-
use rustc_hir::{BorrowKind, Expr, ExprKind};
9+
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability};
1010
use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_middle::ty::{self, Ty};
1212
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -49,10 +49,10 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
4949
if_chain! {
5050
if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty_adjusted(expr).kind();
5151
if let ty::Slice(..) = ty.kind();
52-
if let ExprKind::AddrOf(BorrowKind::Ref, _, ref addressee) = expr.kind;
52+
if let ExprKind::AddrOf(BorrowKind::Ref, mutability, ref addressee) = expr.kind;
5353
if let Some(vec_args) = higher::vec_macro(cx, addressee);
5454
then {
55-
self.check_vec_macro(cx, &vec_args, expr.span);
55+
self.check_vec_macro(cx, &vec_args, mutability, expr.span);
5656
}
5757
}
5858

@@ -70,14 +70,20 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
7070
.ctxt()
7171
.outer_expn_data()
7272
.call_site;
73-
self.check_vec_macro(cx, &vec_args, span);
73+
self.check_vec_macro(cx, &vec_args, Mutability::Not, span);
7474
}
7575
}
7676
}
7777
}
7878

7979
impl UselessVec {
80-
fn check_vec_macro<'tcx>(self, cx: &LateContext<'tcx>, vec_args: &higher::VecArgs<'tcx>, span: Span) {
80+
fn check_vec_macro<'tcx>(
81+
self,
82+
cx: &LateContext<'tcx>,
83+
vec_args: &higher::VecArgs<'tcx>,
84+
mutability: Mutability,
85+
span: Span,
86+
) {
8187
let mut applicability = Applicability::MachineApplicable;
8288
let snippet = match *vec_args {
8389
higher::VecArgs::Repeat(elem, len) => {
@@ -87,11 +93,22 @@ impl UselessVec {
8793
return;
8894
}
8995

90-
format!(
91-
"&[{}; {}]",
92-
snippet_with_applicability(cx, elem.span, "elem", &mut applicability),
93-
snippet_with_applicability(cx, len.span, "len", &mut applicability)
94-
)
96+
match mutability {
97+
Mutability::Mut => {
98+
format!(
99+
"&mut [{}; {}]",
100+
snippet_with_applicability(cx, elem.span, "elem", &mut applicability),
101+
snippet_with_applicability(cx, len.span, "len", &mut applicability)
102+
)
103+
},
104+
Mutability::Not => {
105+
format!(
106+
"&[{}; {}]",
107+
snippet_with_applicability(cx, elem.span, "elem", &mut applicability),
108+
snippet_with_applicability(cx, len.span, "len", &mut applicability)
109+
)
110+
},
111+
}
95112
} else {
96113
return;
97114
}
@@ -104,9 +121,22 @@ impl UselessVec {
104121
}
105122
let span = args[0].span.to(last.span);
106123

107-
format!("&[{}]", snippet_with_applicability(cx, span, "..", &mut applicability))
124+
match mutability {
125+
Mutability::Mut => {
126+
format!(
127+
"&mut [{}]",
128+
snippet_with_applicability(cx, span, "..", &mut applicability)
129+
)
130+
},
131+
Mutability::Not => {
132+
format!("&[{}]", snippet_with_applicability(cx, span, "..", &mut applicability))
133+
},
134+
}
108135
} else {
109-
"&[]".into()
136+
match mutability {
137+
Mutability::Mut => "&mut []".into(),
138+
Mutability::Not => "&[]".into(),
139+
}
110140
}
111141
},
112142
};

tests/ui/vec.fixed

+16
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
struct NonCopy;
77

88
fn on_slice(_: &[u8]) {}
9+
10+
fn on_mut_slice(_: &mut [u8]) {}
11+
912
#[allow(clippy::ptr_arg)]
1013
fn on_vec(_: &Vec<u8>) {}
1114

15+
fn on_mut_vec(_: &mut Vec<u8>) {}
16+
1217
struct Line {
1318
length: usize,
1419
}
@@ -22,28 +27,38 @@ impl Line {
2227
fn main() {
2328
on_slice(&[]);
2429
on_slice(&[]);
30+
on_mut_slice(&mut []);
2531

2632
on_slice(&[1, 2]);
2733
on_slice(&[1, 2]);
34+
on_mut_slice(&mut [1, 2]);
2835

2936
on_slice(&[1, 2]);
3037
on_slice(&[1, 2]);
38+
on_mut_slice(&mut [1, 2]);
3139
#[rustfmt::skip]
3240
on_slice(&[1, 2]);
3341
on_slice(&[1, 2]);
42+
on_mut_slice(&mut [1, 2]);
3443

3544
on_slice(&[1; 2]);
3645
on_slice(&[1; 2]);
46+
on_mut_slice(&mut [1; 2]);
3747

3848
on_vec(&vec![]);
3949
on_vec(&vec![1, 2]);
4050
on_vec(&vec![1; 2]);
51+
on_mut_vec(&mut vec![]);
52+
on_mut_vec(&mut vec![1, 2]);
53+
on_mut_vec(&mut vec![1; 2]);
4154

4255
// Now with non-constant expressions
4356
let line = Line { length: 2 };
4457

4558
on_slice(&vec![2; line.length]);
4659
on_slice(&vec![2; line.length()]);
60+
on_mut_slice(&mut vec![2; line.length]);
61+
on_mut_slice(&mut vec![2; line.length()]);
4762

4863
for a in &[1, 2, 3] {
4964
println!("{:?}", a);
@@ -54,6 +69,7 @@ fn main() {
5469
}
5570

5671
on_vec(&vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
72+
on_mut_vec(&mut vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
5773

5874
// Ok
5975
for a in vec![1; 201] {

tests/ui/vec.rs

+16
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66
struct NonCopy;
77

88
fn on_slice(_: &[u8]) {}
9+
10+
fn on_mut_slice(_: &mut [u8]) {}
11+
912
#[allow(clippy::ptr_arg)]
1013
fn on_vec(_: &Vec<u8>) {}
1114

15+
fn on_mut_vec(_: &mut Vec<u8>) {}
16+
1217
struct Line {
1318
length: usize,
1419
}
@@ -22,28 +27,38 @@ impl Line {
2227
fn main() {
2328
on_slice(&vec![]);
2429
on_slice(&[]);
30+
on_mut_slice(&mut vec![]);
2531

2632
on_slice(&vec![1, 2]);
2733
on_slice(&[1, 2]);
34+
on_mut_slice(&mut vec![1, 2]);
2835

2936
on_slice(&vec![1, 2]);
3037
on_slice(&[1, 2]);
38+
on_mut_slice(&mut vec![1, 2]);
3139
#[rustfmt::skip]
3240
on_slice(&vec!(1, 2));
3341
on_slice(&[1, 2]);
42+
on_mut_slice(&mut vec![1, 2]);
3443

3544
on_slice(&vec![1; 2]);
3645
on_slice(&[1; 2]);
46+
on_mut_slice(&mut vec![1; 2]);
3747

3848
on_vec(&vec![]);
3949
on_vec(&vec![1, 2]);
4050
on_vec(&vec![1; 2]);
51+
on_mut_vec(&mut vec![]);
52+
on_mut_vec(&mut vec![1, 2]);
53+
on_mut_vec(&mut vec![1; 2]);
4154

4255
// Now with non-constant expressions
4356
let line = Line { length: 2 };
4457

4558
on_slice(&vec![2; line.length]);
4659
on_slice(&vec![2; line.length()]);
60+
on_mut_slice(&mut vec![2; line.length]);
61+
on_mut_slice(&mut vec![2; line.length()]);
4762

4863
for a in vec![1, 2, 3] {
4964
println!("{:?}", a);
@@ -54,6 +69,7 @@ fn main() {
5469
}
5570

5671
on_vec(&vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
72+
on_mut_vec(&mut vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
5773

5874
// Ok
5975
for a in vec![1; 201] {

tests/ui/vec.stderr

+37-7
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,70 @@
11
error: useless use of `vec!`
2-
--> $DIR/vec.rs:23:14
2+
--> $DIR/vec.rs:28:14
33
|
44
LL | on_slice(&vec![]);
55
| ^^^^^^^ help: you can use a slice directly: `&[]`
66
|
77
= note: `-D clippy::useless-vec` implied by `-D warnings`
88

99
error: useless use of `vec!`
10-
--> $DIR/vec.rs:26:14
10+
--> $DIR/vec.rs:30:18
11+
|
12+
LL | on_mut_slice(&mut vec![]);
13+
| ^^^^^^^^^^^ help: you can use a slice directly: `&mut []`
14+
15+
error: useless use of `vec!`
16+
--> $DIR/vec.rs:32:14
1117
|
1218
LL | on_slice(&vec![1, 2]);
1319
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
1420

1521
error: useless use of `vec!`
16-
--> $DIR/vec.rs:29:14
22+
--> $DIR/vec.rs:34:18
23+
|
24+
LL | on_mut_slice(&mut vec![1, 2]);
25+
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1, 2]`
26+
27+
error: useless use of `vec!`
28+
--> $DIR/vec.rs:36:14
1729
|
1830
LL | on_slice(&vec![1, 2]);
1931
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
2032

2133
error: useless use of `vec!`
22-
--> $DIR/vec.rs:32:14
34+
--> $DIR/vec.rs:38:18
35+
|
36+
LL | on_mut_slice(&mut vec![1, 2]);
37+
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1, 2]`
38+
39+
error: useless use of `vec!`
40+
--> $DIR/vec.rs:40:14
2341
|
2442
LL | on_slice(&vec!(1, 2));
2543
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
2644

2745
error: useless use of `vec!`
28-
--> $DIR/vec.rs:35:14
46+
--> $DIR/vec.rs:42:18
47+
|
48+
LL | on_mut_slice(&mut vec![1, 2]);
49+
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1, 2]`
50+
51+
error: useless use of `vec!`
52+
--> $DIR/vec.rs:44:14
2953
|
3054
LL | on_slice(&vec![1; 2]);
3155
| ^^^^^^^^^^^ help: you can use a slice directly: `&[1; 2]`
3256

3357
error: useless use of `vec!`
34-
--> $DIR/vec.rs:48:14
58+
--> $DIR/vec.rs:46:18
59+
|
60+
LL | on_mut_slice(&mut vec![1; 2]);
61+
| ^^^^^^^^^^^^^^^ help: you can use a slice directly: `&mut [1; 2]`
62+
63+
error: useless use of `vec!`
64+
--> $DIR/vec.rs:63:14
3565
|
3666
LL | for a in vec![1, 2, 3] {
3767
| ^^^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2, 3]`
3868

39-
error: aborting due to 6 previous errors
69+
error: aborting due to 11 previous errors
4070

0 commit comments

Comments
 (0)