Skip to content

Commit 6f7e89f

Browse files
committed
unused-parens: implement for block return values
1 parent e23dd66 commit 6f7e89f

File tree

9 files changed

+50
-27
lines changed

9 files changed

+50
-27
lines changed

src/librustc/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2410,7 +2410,7 @@ impl<'tcx> AdtDef {
24102410

24112411
#[inline]
24122412
pub fn variant_range(&self) -> Range<VariantIdx> {
2413-
(VariantIdx::new(0)..VariantIdx::new(self.variants.len()))
2413+
VariantIdx::new(0)..VariantIdx::new(self.variants.len())
24142414
}
24152415

24162416
/// Computes the discriminant value used by a specific variant.

src/librustc/ty/sty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ impl<'tcx> GeneratorSubsts<'tcx> {
529529
pub fn variant_range(&self, def_id: DefId, tcx: TyCtxt<'tcx>) -> Range<VariantIdx> {
530530
// FIXME requires optimized MIR
531531
let num_variants = tcx.generator_layout(def_id).variant_fields.len();
532-
(VariantIdx::new(0)..VariantIdx::new(num_variants))
532+
VariantIdx::new(0)..VariantIdx::new(num_variants)
533533
}
534534

535535
/// The discriminant for the given variant. Panics if the `variant_index` is

src/librustc_data_structures/sorted_map.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<K: Ord, V> SortedMap<K, V> {
132132
R: RangeBounds<K>,
133133
{
134134
let (start, end) = self.range_slice_indices(range);
135-
(&self.data[start..end])
135+
&self.data[start..end]
136136
}
137137

138138
#[inline]

src/librustc_lint/unused.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,20 @@ impl EarlyLintPass for UnusedParens {
544544
}
545545

546546
fn check_stmt(&mut self, cx: &EarlyContext<'_>, s: &ast::Stmt) {
547-
if let ast::StmtKind::Local(ref local) = s.kind {
548-
self.check_unused_parens_pat(cx, &local.pat, false, false);
547+
use ast::StmtKind::*;
549548

550-
if let Some(ref value) = local.init {
551-
self.check_unused_parens_expr(cx, &value, "assigned value", false, None, None);
549+
match s.kind {
550+
Local(ref local) => {
551+
self.check_unused_parens_pat(cx, &local.pat, false, false);
552+
553+
if let Some(ref value) = local.init {
554+
self.check_unused_parens_expr(cx, &value, "assigned value", false, None, None);
555+
}
552556
}
557+
Expr(ref expr) => {
558+
self.check_unused_parens_expr(cx, &expr, "block return value", false, None, None);
559+
}
560+
_ => {}
553561
}
554562
}
555563

src/librustc_mir_build/hair/pattern/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1530,7 +1530,7 @@ impl<'tcx> IntRange<'tcx> {
15301530
// 2 -------- // 2 -------
15311531
let (lo, hi) = self.boundaries();
15321532
let (other_lo, other_hi) = other.boundaries();
1533-
(lo == other_hi || hi == other_lo)
1533+
lo == other_hi || hi == other_lo
15341534
}
15351535

15361536
fn to_pat(&self, tcx: TyCtxt<'tcx>) -> Pat<'tcx> {

src/librustc_span/source_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -774,10 +774,10 @@ impl SourceMap {
774774
// searching forwards for boundaries we've got somewhere to search.
775775
let snippet = if let Some(ref src) = local_begin.sf.src {
776776
let len = src.len();
777-
(&src[start_index..len])
777+
&src[start_index..len]
778778
} else if let Some(src) = src.get_source() {
779779
let len = src.len();
780-
(&src[start_index..len])
780+
&src[start_index..len]
781781
} else {
782782
return 1;
783783
};

src/libsyntax/print/pprust.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
548548
let st = match style {
549549
ast::StrStyle::Cooked => (format!("\"{}\"", st.escape_debug())),
550550
ast::StrStyle::Raw(n) => {
551-
(format!(
552-
"r{delim}\"{string}\"{delim}",
553-
delim = "#".repeat(n as usize),
554-
string = st
555-
))
551+
format!("r{delim}\"{string}\"{delim}", delim = "#".repeat(n as usize), string = st)
556552
}
557553
};
558554
self.word(st)

src/test/ui/lint/lint-unnecessary-parens.rs

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ fn unused_parens_around_return_type() -> (u32) { //~ ERROR unnecessary parenthes
1717
panic!()
1818
}
1919

20+
fn unused_parens_around_block_return() -> u32 {
21+
let foo = {
22+
(5) //~ ERROR unnecessary parentheses around block return value
23+
};
24+
(5) //~ ERROR unnecessary parentheses around block return value
25+
}
26+
2027
trait Trait {
2128
fn test(&self);
2229
}

src/test/ui/lint/lint-unnecessary-parens.stderr

+24-12
Original file line numberDiff line numberDiff line change
@@ -22,73 +22,85 @@ error: unnecessary parentheses around type
2222
LL | fn unused_parens_around_return_type() -> (u32) {
2323
| ^^^^^ help: remove these parentheses
2424

25+
error: unnecessary parentheses around block return value
26+
--> $DIR/lint-unnecessary-parens.rs:22:9
27+
|
28+
LL | (5)
29+
| ^^^ help: remove these parentheses
30+
31+
error: unnecessary parentheses around block return value
32+
--> $DIR/lint-unnecessary-parens.rs:24:5
33+
|
34+
LL | (5)
35+
| ^^^ help: remove these parentheses
36+
2537
error: unnecessary parentheses around function argument
26-
--> $DIR/lint-unnecessary-parens.rs:36:9
38+
--> $DIR/lint-unnecessary-parens.rs:43:9
2739
|
2840
LL | bar((true));
2941
| ^^^^^^ help: remove these parentheses
3042

3143
error: unnecessary parentheses around `if` condition
32-
--> $DIR/lint-unnecessary-parens.rs:38:8
44+
--> $DIR/lint-unnecessary-parens.rs:45:8
3345
|
3446
LL | if (true) {}
3547
| ^^^^^^ help: remove these parentheses
3648

3749
error: unnecessary parentheses around `while` condition
38-
--> $DIR/lint-unnecessary-parens.rs:39:11
50+
--> $DIR/lint-unnecessary-parens.rs:46:11
3951
|
4052
LL | while (true) {}
4153
| ^^^^^^ help: remove these parentheses
4254

4355
warning: denote infinite loops with `loop { ... }`
44-
--> $DIR/lint-unnecessary-parens.rs:39:5
56+
--> $DIR/lint-unnecessary-parens.rs:46:5
4557
|
4658
LL | while (true) {}
4759
| ^^^^^^^^^^^^ help: use `loop`
4860
|
4961
= note: `#[warn(while_true)]` on by default
5062

5163
error: unnecessary parentheses around `match` head expression
52-
--> $DIR/lint-unnecessary-parens.rs:41:11
64+
--> $DIR/lint-unnecessary-parens.rs:48:11
5365
|
5466
LL | match (true) {
5567
| ^^^^^^ help: remove these parentheses
5668

5769
error: unnecessary parentheses around `let` head expression
58-
--> $DIR/lint-unnecessary-parens.rs:44:16
70+
--> $DIR/lint-unnecessary-parens.rs:51:16
5971
|
6072
LL | if let 1 = (1) {}
6173
| ^^^ help: remove these parentheses
6274

6375
error: unnecessary parentheses around `let` head expression
64-
--> $DIR/lint-unnecessary-parens.rs:45:19
76+
--> $DIR/lint-unnecessary-parens.rs:52:19
6577
|
6678
LL | while let 1 = (2) {}
6779
| ^^^ help: remove these parentheses
6880

6981
error: unnecessary parentheses around method argument
70-
--> $DIR/lint-unnecessary-parens.rs:59:24
82+
--> $DIR/lint-unnecessary-parens.rs:66:24
7183
|
7284
LL | X { y: false }.foo((true));
7385
| ^^^^^^ help: remove these parentheses
7486

7587
error: unnecessary parentheses around assigned value
76-
--> $DIR/lint-unnecessary-parens.rs:61:18
88+
--> $DIR/lint-unnecessary-parens.rs:68:18
7789
|
7890
LL | let mut _a = (0);
7991
| ^^^ help: remove these parentheses
8092

8193
error: unnecessary parentheses around assigned value
82-
--> $DIR/lint-unnecessary-parens.rs:62:10
94+
--> $DIR/lint-unnecessary-parens.rs:69:10
8395
|
8496
LL | _a = (0);
8597
| ^^^ help: remove these parentheses
8698

8799
error: unnecessary parentheses around assigned value
88-
--> $DIR/lint-unnecessary-parens.rs:63:11
100+
--> $DIR/lint-unnecessary-parens.rs:70:11
89101
|
90102
LL | _a += (1);
91103
| ^^^ help: remove these parentheses
92104

93-
error: aborting due to 13 previous errors
105+
error: aborting due to 15 previous errors
94106

0 commit comments

Comments
 (0)