Skip to content

Commit e3b4989

Browse files
committed
code suggestions for unused-mut, while-true lints; UI test
1 parent 5c9f806 commit e3b4989

File tree

6 files changed

+81
-8
lines changed

6 files changed

+81
-8
lines changed

src/librustc_lint/builtin.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for WhileTrue {
7676
if let hir::ExprLit(ref lit) = cond.node {
7777
if let ast::LitKind::Bool(true) = lit.node {
7878
if lit.span.ctxt() == SyntaxContext::empty() {
79-
cx.span_lint(WHILE_TRUE,
80-
e.span,
81-
"denote infinite loops with loop { ... }");
79+
let msg = "denote infinite loops with `loop { ... }`";
80+
let mut err = cx.struct_span_lint(WHILE_TRUE, e.span, msg);
81+
let condition_span = cx.tcx.sess.codemap().def_span(e.span);
82+
err.span_suggestion_short(condition_span,
83+
"use `loop`",
84+
"loop".to_owned());
85+
err.emit();
8286
}
8387
}
8488
}

src/librustc_lint/unused.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ impl UnusedMut {
7171
let used_mutables = cx.tcx.used_mut_nodes.borrow();
7272
for (_, v) in &mutables {
7373
if !v.iter().any(|e| used_mutables.contains(e)) {
74-
cx.span_lint(UNUSED_MUT,
75-
cx.tcx.hir.span(v[0]),
76-
"variable does not need to be mutable");
74+
let binding_span = cx.tcx.hir.span(v[0]);
75+
let mut_span = cx.tcx.sess.codemap().span_until_char(binding_span, ' ');
76+
let mut err = cx.struct_span_lint(UNUSED_MUT,
77+
binding_span,
78+
"variable does not need to be mutable");
79+
err.span_suggestion_short(mut_span, "remove this `mut`", "".to_owned());
80+
err.emit();
7781
}
7882
}
7983
}

src/test/compile-fail/issue-1962.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// compile-flags: -D while-true
1212
fn main() {
1313
let mut i = 0;
14-
while true { //~ ERROR denote infinite loops with loop
14+
while true { //~ ERROR denote infinite loops with `loop
1515
i += 1;
1616
if i == 5 { break; }
1717
}

src/test/ui/lint/suggestions.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![warn(unused_mut)] // UI tests pass `-A unused`—see Issue #43896
12+
#![feature(no_debug)]
13+
14+
#[no_debug] // should suggest removal of deprecated attribute
15+
fn main() {
16+
while true { // should suggest `loop`
17+
let mut a = (1); // should suggest no `mut`, no parens
18+
println!("{}", a);
19+
}
20+
}

src/test/ui/lint/suggestions.stderr

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
warning: unnecessary parentheses around assigned value
2+
--> $DIR/suggestions.rs:17:21
3+
|
4+
17 | let mut a = (1); // should suggest no `mut`, no parens
5+
| ^^^ help: remove these parentheses
6+
|
7+
= note: #[warn(unused_parens)] on by default
8+
9+
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
10+
--> $DIR/suggestions.rs:14:1
11+
|
12+
14 | #[no_debug] // should suggest removal of deprecated attribute
13+
| ^^^^^^^^^^^ help: remove this attribute
14+
|
15+
= note: #[warn(deprecated)] on by default
16+
17+
warning: denote infinite loops with `loop { ... }`
18+
--> $DIR/suggestions.rs:16:5
19+
|
20+
16 | while true { // should suggest `loop`
21+
| ^---------
22+
| |
23+
| _____help: use `loop`
24+
| |
25+
17 | | let mut a = (1); // should suggest no `mut`, no parens
26+
18 | | println!("{}", a);
27+
19 | | }
28+
| |_____^
29+
|
30+
= note: #[warn(while_true)] on by default
31+
32+
warning: variable does not need to be mutable
33+
--> $DIR/suggestions.rs:17:13
34+
|
35+
17 | let mut a = (1); // should suggest no `mut`, no parens
36+
| ---^^
37+
| |
38+
| help: remove this `mut`
39+
|
40+
note: lint level defined here
41+
--> $DIR/suggestions.rs:11:9
42+
|
43+
11 | #![warn(unused_mut)] // UI tests pass `-A unused`—see Issue #43896
44+
| ^^^^^^^^^^
45+

src/test/ui/path-lookahead.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ warning: unnecessary parentheses around `return` value
22
--> $DIR/path-lookahead.rs:18:10
33
|
44
18 | return (<T as ToString>::to_string(&arg)); //~WARN unnecessary parentheses around `return` value
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses
66
|
77
= note: #[warn(unused_parens)] on by default
88

0 commit comments

Comments
 (0)