From 6f7e89ffe3824be03ea75a602dc46e711d16cb25 Mon Sep 17 00:00:00 2001 From: Tyler Lanphear Date: Thu, 23 Jan 2020 00:42:35 -0500 Subject: [PATCH] unused-parens: implement for block return values --- src/librustc/ty/mod.rs | 2 +- src/librustc/ty/sty.rs | 2 +- src/librustc_data_structures/sorted_map.rs | 2 +- src/librustc_lint/unused.rs | 16 ++++++--- src/librustc_mir_build/hair/pattern/_match.rs | 2 +- src/librustc_span/source_map.rs | 4 +-- src/libsyntax/print/pprust.rs | 6 +--- src/test/ui/lint/lint-unnecessary-parens.rs | 7 ++++ .../ui/lint/lint-unnecessary-parens.stderr | 36 ++++++++++++------- 9 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 0470ab20dc4..e67131b9164 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -2410,7 +2410,7 @@ impl<'tcx> AdtDef { #[inline] pub fn variant_range(&self) -> Range { - (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. diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index 13f623aadb1..837b2fcc500 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -529,7 +529,7 @@ impl<'tcx> GeneratorSubsts<'tcx> { pub fn variant_range(&self, def_id: DefId, tcx: TyCtxt<'tcx>) -> Range { // 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 diff --git a/src/librustc_data_structures/sorted_map.rs b/src/librustc_data_structures/sorted_map.rs index b29ffd75940..08706aac11e 100644 --- a/src/librustc_data_structures/sorted_map.rs +++ b/src/librustc_data_structures/sorted_map.rs @@ -132,7 +132,7 @@ impl SortedMap { R: RangeBounds, { let (start, end) = self.range_slice_indices(range); - (&self.data[start..end]) + &self.data[start..end] } #[inline] diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 15158c09af0..bb2c4fa1aaf 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -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); + } + _ => {} } } diff --git a/src/librustc_mir_build/hair/pattern/_match.rs b/src/librustc_mir_build/hair/pattern/_match.rs index 20183fd55c8..a2ce224904b 100644 --- a/src/librustc_mir_build/hair/pattern/_match.rs +++ b/src/librustc_mir_build/hair/pattern/_match.rs @@ -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> { diff --git a/src/librustc_span/source_map.rs b/src/librustc_span/source_map.rs index 9c7c0f0c8b0..e0b93b9ce25 100644 --- a/src/librustc_span/source_map.rs +++ b/src/librustc_span/source_map.rs @@ -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; }; diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index f0ef33e2f62..d6f18fda8b2 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -548,11 +548,7 @@ pub trait PrintState<'a>: std::ops::Deref + 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) diff --git a/src/test/ui/lint/lint-unnecessary-parens.rs b/src/test/ui/lint/lint-unnecessary-parens.rs index 12ffb6d3c66..4e8339a8e6b 100644 --- a/src/test/ui/lint/lint-unnecessary-parens.rs +++ b/src/test/ui/lint/lint-unnecessary-parens.rs @@ -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); } diff --git a/src/test/ui/lint/lint-unnecessary-parens.stderr b/src/test/ui/lint/lint-unnecessary-parens.stderr index 541ae7aa4b5..ea58220d20c 100644 --- a/src/test/ui/lint/lint-unnecessary-parens.stderr +++ b/src/test/ui/lint/lint-unnecessary-parens.stderr @@ -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