code suggestions for unused-mut, while-true lints; UI test

This commit is contained in:
Zack M. Davis 2017-09-29 23:52:51 -07:00
parent 5c9f806d78
commit e3b498971d
6 changed files with 81 additions and 8 deletions

View File

@ -76,9 +76,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for WhileTrue {
if let hir::ExprLit(ref lit) = cond.node {
if let ast::LitKind::Bool(true) = lit.node {
if lit.span.ctxt() == SyntaxContext::empty() {
cx.span_lint(WHILE_TRUE,
e.span,
"denote infinite loops with loop { ... }");
let msg = "denote infinite loops with `loop { ... }`";
let mut err = cx.struct_span_lint(WHILE_TRUE, e.span, msg);
let condition_span = cx.tcx.sess.codemap().def_span(e.span);
err.span_suggestion_short(condition_span,
"use `loop`",
"loop".to_owned());
err.emit();
}
}
}

View File

@ -71,9 +71,13 @@ impl UnusedMut {
let used_mutables = cx.tcx.used_mut_nodes.borrow();
for (_, v) in &mutables {
if !v.iter().any(|e| used_mutables.contains(e)) {
cx.span_lint(UNUSED_MUT,
cx.tcx.hir.span(v[0]),
"variable does not need to be mutable");
let binding_span = cx.tcx.hir.span(v[0]);
let mut_span = cx.tcx.sess.codemap().span_until_char(binding_span, ' ');
let mut err = cx.struct_span_lint(UNUSED_MUT,
binding_span,
"variable does not need to be mutable");
err.span_suggestion_short(mut_span, "remove this `mut`", "".to_owned());
err.emit();
}
}
}

View File

@ -11,7 +11,7 @@
// compile-flags: -D while-true
fn main() {
let mut i = 0;
while true { //~ ERROR denote infinite loops with loop
while true { //~ ERROR denote infinite loops with `loop
i += 1;
if i == 5 { break; }
}

View File

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

View File

@ -0,0 +1,45 @@
warning: unnecessary parentheses around assigned value
--> $DIR/suggestions.rs:17:21
|
17 | let mut a = (1); // should suggest no `mut`, no parens
| ^^^ help: remove these parentheses
|
= note: #[warn(unused_parens)] on by default
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
--> $DIR/suggestions.rs:14:1
|
14 | #[no_debug] // should suggest removal of deprecated attribute
| ^^^^^^^^^^^ help: remove this attribute
|
= note: #[warn(deprecated)] on by default
warning: denote infinite loops with `loop { ... }`
--> $DIR/suggestions.rs:16:5
|
16 | while true { // should suggest `loop`
| ^---------
| |
| _____help: use `loop`
| |
17 | | let mut a = (1); // should suggest no `mut`, no parens
18 | | println!("{}", a);
19 | | }
| |_____^
|
= note: #[warn(while_true)] on by default
warning: variable does not need to be mutable
--> $DIR/suggestions.rs:17:13
|
17 | let mut a = (1); // should suggest no `mut`, no parens
| ---^^
| |
| help: remove this `mut`
|
note: lint level defined here
--> $DIR/suggestions.rs:11:9
|
11 | #![warn(unused_mut)] // UI tests pass `-A unused`—see Issue #43896
| ^^^^^^^^^^

View File

@ -2,7 +2,7 @@ warning: unnecessary parentheses around `return` value
--> $DIR/path-lookahead.rs:18:10
|
18 | return (<T as ToString>::to_string(&arg)); //~WARN unnecessary parentheses around `return` value
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove these parentheses
|
= note: #[warn(unused_parens)] on by default