Skip to content

Commit 616b66d

Browse files
committed
Auto merge of #47465 - estebank:include-space-after-mut, r=nikomatsakis
Include space in suggestion `mut` in bindings Fix #46614.
2 parents 6741e41 + df412ce commit 616b66d

File tree

4 files changed

+72
-32
lines changed

4 files changed

+72
-32
lines changed

src/librustc_borrowck/borrowck/unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a, 'tcx> UnusedMutCx<'a, 'tcx> {
7777
continue
7878
}
7979

80-
let mut_span = tcx.sess.codemap().span_until_char(ids[0].2, ' ');
80+
let mut_span = tcx.sess.codemap().span_until_non_whitespace(ids[0].2);
8181

8282
// Ok, every name wasn't used mutably, so issue a warning that this
8383
// didn't need to be mutable.

src/libsyntax/codemap.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,30 @@ impl CodeMap {
593593
}
594594
}
595595

596-
/// Given a `Span`, try to get a shorter span ending just after the first
597-
/// occurrence of `char` `c`.
596+
/// Given a `Span`, get a new `Span` covering the first token and all its trailing whitespace or
597+
/// the original `Span`.
598+
///
599+
/// If `sp` points to `"let mut x"`, then a span pointing at `"let "` will be returned.
600+
pub fn span_until_non_whitespace(&self, sp: Span) -> Span {
601+
if let Ok(snippet) = self.span_to_snippet(sp) {
602+
let mut offset = 0;
603+
// get the bytes width of all the non-whitespace characters
604+
for c in snippet.chars().take_while(|c| !c.is_whitespace()) {
605+
offset += c.len_utf8();
606+
}
607+
// get the bytes width of all the whitespace characters after that
608+
for c in snippet[offset..].chars().take_while(|c| c.is_whitespace()) {
609+
offset += c.len_utf8();
610+
}
611+
if offset > 1 {
612+
return sp.with_hi(BytePos(sp.lo().0 + offset as u32));
613+
}
614+
}
615+
sp
616+
}
617+
618+
/// Given a `Span`, try to get a shorter span ending just after the first occurrence of `char`
619+
/// `c`.
598620
pub fn span_through_char(&self, sp: Span, c: char) -> Span {
599621
if let Ok(snippet) = self.span_to_snippet(sp) {
600622
if let Some(offset) = snippet.find(c) {

src/test/ui/lint/suggestions.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// ignore-tidy-tab
12+
1113
#![warn(unused_mut, unused_parens)] // UI tests pass `-A unused`—see Issue #43896
1214
#![feature(no_debug)]
1315

@@ -46,11 +48,15 @@ fn main() {
4648
let mut a = (1); // should suggest no `mut`, no parens
4749
//~^ WARN does not need to be mutable
4850
//~| WARN unnecessary parentheses
51+
// the line after `mut` has a `\t` at the beginning, this is on purpose
52+
let mut
53+
b = 1;
54+
//~^^ WARN does not need to be mutable
4955
let d = Equinox { warp_factor: 9.975 };
5056
match d {
5157
Equinox { warp_factor: warp_factor } => {} // should suggest shorthand
5258
//~^ WARN this pattern is redundant
5359
}
54-
println!("{}", a);
60+
println!("{} {}", a, b);
5561
}
5662
}

src/test/ui/lint/suggestions.stderr

+40-28
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,113 @@
11
warning: unnecessary parentheses around assigned value
2-
--> $DIR/suggestions.rs:46:21
2+
--> $DIR/suggestions.rs:48:21
33
|
4-
46 | let mut a = (1); // should suggest no `mut`, no parens
4+
48 | let mut a = (1); // should suggest no `mut`, no parens
55
| ^^^ help: remove these parentheses
66
|
77
note: lint level defined here
8-
--> $DIR/suggestions.rs:11:21
8+
--> $DIR/suggestions.rs:13:21
99
|
10-
11 | #![warn(unused_mut, unused_parens)] // UI tests pass `-A unused`—see Issue #43896
10+
13 | #![warn(unused_mut, unused_parens)] // UI tests pass `-A unused`—see Issue #43896
1111
| ^^^^^^^^^^^^^
1212

1313
warning: use of deprecated attribute `no_debug`: the `#[no_debug]` attribute was an experimental feature that has been deprecated due to lack of demand. See https://github.com/rust-lang/rust/issues/29721
14-
--> $DIR/suggestions.rs:41:1
14+
--> $DIR/suggestions.rs:43:1
1515
|
16-
41 | #[no_debug] // should suggest removal of deprecated attribute
16+
43 | #[no_debug] // should suggest removal of deprecated attribute
1717
| ^^^^^^^^^^^ help: remove this attribute
1818
|
1919
= note: #[warn(deprecated)] on by default
2020

2121
warning: variable does not need to be mutable
22-
--> $DIR/suggestions.rs:46:13
22+
--> $DIR/suggestions.rs:48:13
2323
|
24-
46 | let mut a = (1); // should suggest no `mut`, no parens
25-
| ---^^
24+
48 | let mut a = (1); // should suggest no `mut`, no parens
25+
| ----^
2626
| |
2727
| help: remove this `mut`
2828
|
2929
note: lint level defined here
30-
--> $DIR/suggestions.rs:11:9
30+
--> $DIR/suggestions.rs:13:9
3131
|
32-
11 | #![warn(unused_mut, unused_parens)] // UI tests pass `-A unused`—see Issue #43896
32+
13 | #![warn(unused_mut, unused_parens)] // UI tests pass `-A unused`—see Issue #43896
3333
| ^^^^^^^^^^
3434

35+
warning: variable does not need to be mutable
36+
--> $DIR/suggestions.rs:52:13
37+
|
38+
52 | let mut
39+
| _____________^
40+
| |_____________|
41+
| ||
42+
53 | || b = 1;
43+
| ||____________-^
44+
| |____________|
45+
| help: remove this `mut`
46+
3547
warning: static is marked #[no_mangle], but not exported
36-
--> $DIR/suggestions.rs:14:14
48+
--> $DIR/suggestions.rs:16:14
3749
|
38-
14 | #[no_mangle] static SHENZHOU: usize = 1; // should suggest `pub`
50+
16 | #[no_mangle] static SHENZHOU: usize = 1; // should suggest `pub`
3951
| -^^^^^^^^^^^^^^^^^^^^^^^^^^
4052
| |
4153
| help: try making it public: `pub`
4254
|
4355
= note: #[warn(private_no_mangle_statics)] on by default
4456

4557
error: const items should never be #[no_mangle]
46-
--> $DIR/suggestions.rs:16:14
58+
--> $DIR/suggestions.rs:18:14
4759
|
48-
16 | #[no_mangle] const DISCOVERY: usize = 1; // should suggest `pub static` rather than `const`
60+
18 | #[no_mangle] const DISCOVERY: usize = 1; // should suggest `pub static` rather than `const`
4961
| -----^^^^^^^^^^^^^^^^^^^^^^
5062
| |
5163
| help: try a static value: `pub static`
5264
|
5365
= note: #[deny(no_mangle_const_items)] on by default
5466

5567
warning: functions generic over types must be mangled
56-
--> $DIR/suggestions.rs:20:1
68+
--> $DIR/suggestions.rs:22:1
5769
|
58-
19 | #[no_mangle] // should suggest removal (generics can't be no-mangle)
70+
21 | #[no_mangle] // should suggest removal (generics can't be no-mangle)
5971
| ------------ help: remove this attribute
60-
20 | pub fn defiant<T>(_t: T) {}
72+
22 | pub fn defiant<T>(_t: T) {}
6173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6274
|
6375
= note: #[warn(no_mangle_generic_items)] on by default
6476

6577
warning: function is marked #[no_mangle], but not exported
66-
--> $DIR/suggestions.rs:24:1
78+
--> $DIR/suggestions.rs:26:1
6779
|
68-
24 | fn rio_grande() {} // should suggest `pub`
80+
26 | fn rio_grande() {} // should suggest `pub`
6981
| -^^^^^^^^^^^^^^^^^
7082
| |
7183
| help: try making it public: `pub`
7284
|
7385
= note: #[warn(private_no_mangle_fns)] on by default
7486

7587
warning: static is marked #[no_mangle], but not exported
76-
--> $DIR/suggestions.rs:31:18
88+
--> $DIR/suggestions.rs:33:18
7789
|
78-
31 | #[no_mangle] pub static DAUNTLESS: bool = true;
90+
33 | #[no_mangle] pub static DAUNTLESS: bool = true;
7991
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8092

8193
warning: function is marked #[no_mangle], but not exported
82-
--> $DIR/suggestions.rs:33:18
94+
--> $DIR/suggestions.rs:35:18
8395
|
84-
33 | #[no_mangle] pub fn val_jean() {}
96+
35 | #[no_mangle] pub fn val_jean() {}
8597
| ^^^^^^^^^^^^^^^^^^^^
8698

8799
warning: denote infinite loops with `loop { ... }`
88-
--> $DIR/suggestions.rs:44:5
100+
--> $DIR/suggestions.rs:46:5
89101
|
90-
44 | while true { // should suggest `loop`
102+
46 | while true { // should suggest `loop`
91103
| ^^^^^^^^^^ help: use `loop`
92104
|
93105
= note: #[warn(while_true)] on by default
94106

95107
warning: the `warp_factor:` in this pattern is redundant
96-
--> $DIR/suggestions.rs:51:23
108+
--> $DIR/suggestions.rs:57:23
97109
|
98-
51 | Equinox { warp_factor: warp_factor } => {} // should suggest shorthand
110+
57 | Equinox { warp_factor: warp_factor } => {} // should suggest shorthand
99111
| ------------^^^^^^^^^^^^
100112
| |
101113
| help: remove this

0 commit comments

Comments
 (0)