unused-parens: implement for block return values

This commit is contained in:
Tyler Lanphear 2020-01-23 00:42:35 -05:00
parent e23dd6687f
commit 6f7e89ffe3
9 changed files with 50 additions and 27 deletions

View File

@ -2410,7 +2410,7 @@ impl<'tcx> AdtDef {
#[inline]
pub fn variant_range(&self) -> Range<VariantIdx> {
(VariantIdx::new(0)..VariantIdx::new(self.variants.len()))
VariantIdx::new(0)..VariantIdx::new(self.variants.len())
}
/// Computes the discriminant value used by a specific variant.

View File

@ -529,7 +529,7 @@ impl<'tcx> GeneratorSubsts<'tcx> {
pub fn variant_range(&self, def_id: DefId, tcx: TyCtxt<'tcx>) -> Range<VariantIdx> {
// FIXME requires optimized MIR
let num_variants = tcx.generator_layout(def_id).variant_fields.len();
(VariantIdx::new(0)..VariantIdx::new(num_variants))
VariantIdx::new(0)..VariantIdx::new(num_variants)
}
/// The discriminant for the given variant. Panics if the `variant_index` is

View File

@ -132,7 +132,7 @@ impl<K: Ord, V> SortedMap<K, V> {
R: RangeBounds<K>,
{
let (start, end) = self.range_slice_indices(range);
(&self.data[start..end])
&self.data[start..end]
}
#[inline]

View File

@ -544,12 +544,20 @@ impl EarlyLintPass for UnusedParens {
}
fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
if let ast::StmtKind::Local(ref local) = s.kind {
self.check_unused_parens_pat(cx, &local.pat, false, false);
use ast::StmtKind::*;
if let Some(ref value) = local.init {
self.check_unused_parens_expr(cx, &value, "assigned value", false, None, None);
match s.kind {
Local(ref local) => {
self.check_unused_parens_pat(cx, &local.pat, false, false);
if let Some(ref value) = local.init {
self.check_unused_parens_expr(cx, &value, "assigned value", false, None, None);
}
}
Expr(ref expr) => {
self.check_unused_parens_expr(cx, &expr, "block return value", false, None, None);
}
_ => {}
}
}

View File

@ -1530,7 +1530,7 @@ impl<'tcx> IntRange<'tcx> {
// 2 -------- // 2 -------
let (lo, hi) = self.boundaries();
let (other_lo, other_hi) = other.boundaries();
(lo == other_hi || hi == other_lo)
lo == other_hi || hi == other_lo
}
fn to_pat(&self, tcx: TyCtxt<'tcx>) -> Pat<'tcx> {

View File

@ -774,10 +774,10 @@ impl SourceMap {
// searching forwards for boundaries we've got somewhere to search.
let snippet = if let Some(ref src) = local_begin.sf.src {
let len = src.len();
(&src[start_index..len])
&src[start_index..len]
} else if let Some(src) = src.get_source() {
let len = src.len();
(&src[start_index..len])
&src[start_index..len]
} else {
return 1;
};

View File

@ -548,11 +548,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
let st = match style {
ast::StrStyle::Cooked => (format!("\"{}\"", st.escape_debug())),
ast::StrStyle::Raw(n) => {
(format!(
"r{delim}\"{string}\"{delim}",
delim = "#".repeat(n as usize),
string = st
))
format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = st)
}
};
self.word(st)

View File

@ -17,6 +17,13 @@ fn unused_parens_around_return_type() -> (u32) { //~ ERROR unnecessary parenthes
panic!()
}
fn unused_parens_around_block_return() -> u32 {
let foo = {
(5) //~ ERROR unnecessary parentheses around block return value
};
(5) //~ ERROR unnecessary parentheses around block return value
}
trait Trait {
fn test(&self);
}

View File

@ -22,26 +22,38 @@ error: unnecessary parentheses around type
LL | fn unused_parens_around_return_type() -> (u32) {
| ^^^^^ help: remove these parentheses
error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:22:9
|
LL | (5)
| ^^^ help: remove these parentheses
error: unnecessary parentheses around block return value
--> $DIR/lint-unnecessary-parens.rs:24:5
|
LL | (5)
| ^^^ help: remove these parentheses
error: unnecessary parentheses around function argument
--> $DIR/lint-unnecessary-parens.rs:36:9
--> $DIR/lint-unnecessary-parens.rs:43:9
|
LL | bar((true));
| ^^^^^^ help: remove these parentheses
error: unnecessary parentheses around `if` condition
--> $DIR/lint-unnecessary-parens.rs:38:8
--> $DIR/lint-unnecessary-parens.rs:45:8
|
LL | if (true) {}
| ^^^^^^ help: remove these parentheses
error: unnecessary parentheses around `while` condition
--> $DIR/lint-unnecessary-parens.rs:39:11
--> $DIR/lint-unnecessary-parens.rs:46:11
|
LL | while (true) {}
| ^^^^^^ help: remove these parentheses
warning: denote infinite loops with `loop { ... }`
--> $DIR/lint-unnecessary-parens.rs:39:5
--> $DIR/lint-unnecessary-parens.rs:46:5
|
LL | while (true) {}
| ^^^^^^^^^^^^ help: use `loop`
@ -49,46 +61,46 @@ LL | while (true) {}
= note: `#[warn(while_true)]` on by default
error: unnecessary parentheses around `match` head expression
--> $DIR/lint-unnecessary-parens.rs:41:11
--> $DIR/lint-unnecessary-parens.rs:48:11
|
LL | match (true) {
| ^^^^^^ help: remove these parentheses
error: unnecessary parentheses around `let` head expression
--> $DIR/lint-unnecessary-parens.rs:44:16
--> $DIR/lint-unnecessary-parens.rs:51:16
|
LL | if let 1 = (1) {}
| ^^^ help: remove these parentheses
error: unnecessary parentheses around `let` head expression
--> $DIR/lint-unnecessary-parens.rs:45:19
--> $DIR/lint-unnecessary-parens.rs:52:19
|
LL | while let 1 = (2) {}
| ^^^ help: remove these parentheses
error: unnecessary parentheses around method argument
--> $DIR/lint-unnecessary-parens.rs:59:24
--> $DIR/lint-unnecessary-parens.rs:66:24
|
LL | X { y: false }.foo((true));
| ^^^^^^ help: remove these parentheses
error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:61:18
--> $DIR/lint-unnecessary-parens.rs:68:18
|
LL | let mut _a = (0);
| ^^^ help: remove these parentheses
error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:62:10
--> $DIR/lint-unnecessary-parens.rs:69:10
|
LL | _a = (0);
| ^^^ help: remove these parentheses
error: unnecessary parentheses around assigned value
--> $DIR/lint-unnecessary-parens.rs:63:11
--> $DIR/lint-unnecessary-parens.rs:70:11
|
LL | _a += (1);
| ^^^ help: remove these parentheses
error: aborting due to 13 previous errors
error: aborting due to 15 previous errors