Skip to content

Commit 43cffe9

Browse files
committed
auto merge of #11663 : huonw/rust/paren-lint, r=cmr
The parens in `if (true) {}` are not necessary, so we'll warn about them. cc #3070 and #11432
2 parents 40df5a2 + 39713b8 commit 43cffe9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+143
-99
lines changed

src/compiletest/compiletest.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,9 @@ pub fn parse_config(args: ~[~str]) -> config {
140140
adb_test_dir:
141141
opt_str2(matches.opt_str("adb-test-dir")).to_str(),
142142
adb_device_status:
143-
if (opt_str2(matches.opt_str("target")) ==
144-
~"arm-linux-androideabi") {
145-
if (opt_str2(matches.opt_str("adb-test-dir")) !=
146-
~"(none)" &&
147-
opt_str2(matches.opt_str("adb-test-dir")) !=
148-
~"") { true }
149-
else { false }
150-
} else { false },
143+
"arm-linux-androideabi" == opt_str2(matches.opt_str("target")) &&
144+
"(none)" != opt_str2(matches.opt_str("adb-test-dir")) &&
145+
!opt_str2(matches.opt_str("adb-test-dir")).is_empty(),
151146
test_shard: test::opt_shard(matches.opt_str("test-shard")),
152147
verbose: matches.opt_present("verbose")
153148
}

src/compiletest/runtest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,9 @@ fn check_expected_errors(expected_errors: ~[errors::ExpectedError],
532532
if !found_flags[i] {
533533
debug!("prefix={} ee.kind={} ee.msg={} line={}",
534534
prefixes[i], ee.kind, ee.msg, line);
535-
if (prefix_matches(line, prefixes[i]) &&
535+
if prefix_matches(line, prefixes[i]) &&
536536
line.contains(ee.kind) &&
537-
line.contains(ee.msg)) {
537+
line.contains(ee.msg) {
538538
found_flags[i] = true;
539539
was_expected = true;
540540
break;

src/libextra/ebml.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1025,15 +1025,15 @@ mod bench {
10251025
pub fn vuint_at_A_aligned(bh: &mut BenchHarness) {
10261026
use std::vec;
10271027
let data = vec::from_fn(4*100, |i| {
1028-
match (i % 2) {
1028+
match i % 2 {
10291029
0 => 0x80u8,
10301030
_ => i as u8,
10311031
}
10321032
});
10331033
let mut sum = 0u;
10341034
bh.iter(|| {
10351035
let mut i = 0;
1036-
while (i < data.len()) {
1036+
while i < data.len() {
10371037
sum += reader::vuint_at(data, i).val;
10381038
i += 4;
10391039
}
@@ -1044,15 +1044,15 @@ mod bench {
10441044
pub fn vuint_at_A_unaligned(bh: &mut BenchHarness) {
10451045
use std::vec;
10461046
let data = vec::from_fn(4*100+1, |i| {
1047-
match (i % 2) {
1047+
match i % 2 {
10481048
1 => 0x80u8,
10491049
_ => i as u8
10501050
}
10511051
});
10521052
let mut sum = 0u;
10531053
bh.iter(|| {
10541054
let mut i = 1;
1055-
while (i < data.len()) {
1055+
while i < data.len() {
10561056
sum += reader::vuint_at(data, i).val;
10571057
i += 4;
10581058
}
@@ -1063,7 +1063,7 @@ mod bench {
10631063
pub fn vuint_at_D_aligned(bh: &mut BenchHarness) {
10641064
use std::vec;
10651065
let data = vec::from_fn(4*100, |i| {
1066-
match (i % 4) {
1066+
match i % 4 {
10671067
0 => 0x10u8,
10681068
3 => i as u8,
10691069
_ => 0u8
@@ -1072,7 +1072,7 @@ mod bench {
10721072
let mut sum = 0u;
10731073
bh.iter(|| {
10741074
let mut i = 0;
1075-
while (i < data.len()) {
1075+
while i < data.len() {
10761076
sum += reader::vuint_at(data, i).val;
10771077
i += 4;
10781078
}
@@ -1083,7 +1083,7 @@ mod bench {
10831083
pub fn vuint_at_D_unaligned(bh: &mut BenchHarness) {
10841084
use std::vec;
10851085
let data = vec::from_fn(4*100+1, |i| {
1086-
match (i % 4) {
1086+
match i % 4 {
10871087
1 => 0x10u8,
10881088
0 => i as u8,
10891089
_ => 0u8
@@ -1092,7 +1092,7 @@ mod bench {
10921092
let mut sum = 0u;
10931093
bh.iter(|| {
10941094
let mut i = 1;
1095-
while (i < data.len()) {
1095+
while i < data.len() {
10961096
sum += reader::vuint_at(data, i).val;
10971097
i += 4;
10981098
}

src/libextra/enum_set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl<E:CLike> Items<E> {
114114

115115
impl<E:CLike> Iterator<E> for Items<E> {
116116
fn next(&mut self) -> Option<E> {
117-
if (self.bits == 0) {
117+
if self.bits == 0 {
118118
return None;
119119
}
120120

src/libextra/getopts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl Matches {
195195

196196
fn opt_val(&self, nm: &str) -> Option<Optval> {
197197
let vals = self.opt_vals(nm);
198-
if (vals.is_empty()) {
198+
if vals.is_empty() {
199199
None
200200
} else {
201201
Some(vals[0].clone())
@@ -797,7 +797,7 @@ pub mod groups {
797797
let slice: || = || { cont = it(ss.slice(slice_start, last_end)) };
798798

799799
// if the limit is larger than the string, lower it to save cycles
800-
if (lim >= fake_i) {
800+
if lim >= fake_i {
801801
lim = fake_i;
802802
}
803803

src/libextra/json.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ impl<T : Iterator<char>> Parser<T> {
929929
return self.error(~"EOF while parsing string");
930930
}
931931

932-
if (escape) {
932+
if escape {
933933
match self.ch {
934934
'"' => res.push_char('"'),
935935
'\\' => res.push_char('\\'),
@@ -1360,7 +1360,7 @@ impl serialize::Decoder for Decoder {
13601360
/// Test if two json values are less than one another
13611361
impl Ord for Json {
13621362
fn lt(&self, other: &Json) -> bool {
1363-
match (*self) {
1363+
match *self {
13641364
Number(f0) => {
13651365
match *other {
13661366
Number(f1) => f0 < f1,

src/libextra/num/bigint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,9 @@ impl ToPrimitive for BigUint {
561561
impl FromPrimitive for BigUint {
562562
#[inline]
563563
fn from_i64(n: i64) -> Option<BigUint> {
564-
if (n > 0) {
564+
if n > 0 {
565565
FromPrimitive::from_u64(n as u64)
566-
} else if (n == 0) {
566+
} else if n == 0 {
567567
Some(Zero::zero())
568568
} else {
569569
None

src/libextra/terminfo/parser/compiled.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub fn parse(file: &mut io::Reader,
178178

179179
// Check magic number
180180
let magic = file.read_le_u16();
181-
if (magic != 0x011A) {
181+
if magic != 0x011A {
182182
return Err(format!("invalid magic number: expected {:x} but found {:x}",
183183
0x011A, magic as uint));
184184
}

src/libextra/time.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ pub fn strptime(s: &str, format: &str) -> Result<Tm, ~str> {
808808
/// Formats the time according to the format string.
809809
pub fn strftime(format: &str, tm: &Tm) -> ~str {
810810
fn days_in_year(year: int) -> i32 {
811-
if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
811+
if (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)) {
812812
366 /* Days in a leap year */
813813
} else {
814814
365 /* Days in a non-leap year */
@@ -838,14 +838,14 @@ pub fn strftime(format: &str, tm: &Tm) -> ~str {
838838
let mut year: int = tm.tm_year as int + 1900;
839839
let mut days: int = iso_week_days (tm.tm_yday, tm.tm_wday);
840840

841-
if (days < 0) {
841+
if days < 0 {
842842
/* This ISO week belongs to the previous year. */
843843
year -= 1;
844844
days = iso_week_days (tm.tm_yday + (days_in_year(year)), tm.tm_wday);
845845
} else {
846846
let d: int = iso_week_days (tm.tm_yday - (days_in_year(year)),
847847
tm.tm_wday);
848-
if (0 <= d) {
848+
if 0 <= d {
849849
/* This ISO week belongs to the next year. */
850850
year += 1;
851851
days = d;

src/libextra/uuid.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -614,16 +614,16 @@ mod test {
614614

615615
// Test error reporting
616616
let e = Uuid::parse_string("67e5504410b1426f9247bb680e5fe0c").unwrap_err();
617-
assert!(match(e){ ErrorInvalidLength(n) => n==31, _ => false });
617+
assert!(match e { ErrorInvalidLength(n) => n==31, _ => false });
618618

619619
let e = Uuid::parse_string("67e550X410b1426f9247bb680e5fe0cd").unwrap_err();
620-
assert!(match(e){ ErrorInvalidCharacter(c, n) => c=='X' && n==6, _ => false });
620+
assert!(match e { ErrorInvalidCharacter(c, n) => c=='X' && n==6, _ => false });
621621

622622
let e = Uuid::parse_string("67e550-4105b1426f9247bb680e5fe0c").unwrap_err();
623-
assert!(match(e){ ErrorInvalidGroups(n) => n==2, _ => false });
623+
assert!(match e { ErrorInvalidGroups(n) => n==2, _ => false });
624624

625625
let e = Uuid::parse_string("F9168C5E-CEB2-4faa-B6BF1-02BF39FA1E4").unwrap_err();
626-
assert!(match(e){ ErrorInvalidGroupLength(g, n, e) => g==3 && n==5 && e==4, _ => false });
626+
assert!(match e { ErrorInvalidGroupLength(g, n, e) => g==3 && n==5 && e==4, _ => false });
627627
}
628628

629629
#[test]

src/libgreen/sched.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ mod test {
13231323
fn roundtrip(id: int, n_tasks: int,
13241324
p: &Port<(int, Chan<()>)>,
13251325
ch: &Chan<(int, Chan<()>)>) {
1326-
while (true) {
1326+
loop {
13271327
match p.recv() {
13281328
(1, end_chan) => {
13291329
debug!("{}\n", id);

src/libnative/io/file.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,11 @@ pub fn readdir(p: &CString) -> IoResult<~[Path]> {
508508

509509
let dir_ptr = p.with_ref(|buf| opendir(buf));
510510

511-
if (dir_ptr as uint != 0) {
511+
if dir_ptr as uint != 0 {
512512
let mut paths = ~[];
513513
debug!("os::list_dir -- opendir() SUCCESS");
514514
let mut entry_ptr = readdir(dir_ptr);
515-
while (entry_ptr as uint != 0) {
515+
while entry_ptr as uint != 0 {
516516
let cstr = CString::new(rust_list_dir_val(entry_ptr), false);
517517
paths.push(Path::new(cstr));
518518
entry_ptr = readdir(dir_ptr);

src/librustc/metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1956,7 +1956,7 @@ fn encode_metadata_inner(wr: &mut MemWriter, parms: EncodeParams, crate: &Crate)
19561956

19571957
ecx.stats.total_bytes.set(ebml_w.writer.tell());
19581958

1959-
if (tcx.sess.meta_stats()) {
1959+
if tcx.sess.meta_stats() {
19601960
for e in ebml_w.writer.get_ref().iter() {
19611961
if *e == 0 {
19621962
ecx.stats.zero_bytes.set(ecx.stats.zero_bytes.get() + 1);

src/librustc/middle/check_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, pats: ~[@Pat]) {
175175
useful(ty, ref ctor) => {
176176
match ty::get(ty).sty {
177177
ty::ty_bool => {
178-
match (*ctor) {
178+
match *ctor {
179179
val(const_bool(true)) => Some(@"true"),
180180
val(const_bool(false)) => Some(@"false"),
181181
_ => None

src/librustc/middle/lint.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub enum Lint {
7878
NonCamelCaseTypes,
7979
NonUppercaseStatics,
8080
NonUppercasePatternStatics,
81+
UnnecessaryParens,
8182
TypeLimits,
8283
TypeOverflow,
8384
UnusedUnsafe,
@@ -162,7 +163,7 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
162163
("while_true",
163164
LintSpec {
164165
lint: WhileTrue,
165-
desc: "suggest using loop { } instead of while(true) { }",
166+
desc: "suggest using `loop { }` instead of `while true { }`",
166167
default: warn
167168
}),
168169

@@ -201,6 +202,13 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
201202
default: warn
202203
}),
203204

205+
("unnecessary_parens",
206+
LintSpec {
207+
lint: UnnecessaryParens,
208+
desc: "`if`, `match`, `while` and `return` do not need parentheses",
209+
default: warn
210+
}),
211+
204212
("managed_heap_memory",
205213
LintSpec {
206214
lint: ManagedHeapMemory,
@@ -1080,6 +1088,24 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
10801088
}
10811089
}
10821090

1091+
fn check_unnecessary_parens(cx: &Context, e: &ast::Expr) {
1092+
let (value, msg) = match e.node {
1093+
ast::ExprIf(cond, _, _) => (cond, "`if` condition"),
1094+
ast::ExprWhile(cond, _) => (cond, "`while` condition"),
1095+
ast::ExprMatch(head, _) => (head, "`match` head expression"),
1096+
ast::ExprRet(Some(value)) => (value, "`return` value"),
1097+
_ => return
1098+
};
1099+
1100+
match value.node {
1101+
ast::ExprParen(_) => {
1102+
cx.span_lint(UnnecessaryParens, value.span,
1103+
format!("unnecessary parentheses around {}", msg))
1104+
}
1105+
_ => {}
1106+
}
1107+
}
1108+
10831109
fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
10841110
match e.node {
10851111
// Don't warn about generated blocks, that'll just pollute the output.
@@ -1438,6 +1464,7 @@ impl<'a> Visitor<()> for Context<'a> {
14381464

14391465
check_while_true_expr(self, e);
14401466
check_stability(self, e);
1467+
check_unnecessary_parens(self, e);
14411468
check_unused_unsafe(self, e);
14421469
check_unsafe_block(self, e);
14431470
check_unnecessary_allocation(self, e);

src/librustc/middle/resolve.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ impl Resolver {
10391039
let mut duplicate_type = NoError;
10401040
let ns = match duplicate_checking_mode {
10411041
ForbidDuplicateModules => {
1042-
if (child.get_module_if_available().is_some()) {
1042+
if child.get_module_if_available().is_some() {
10431043
duplicate_type = ModuleError;
10441044
}
10451045
Some(TypeNS)
@@ -1074,7 +1074,7 @@ impl Resolver {
10741074
}
10751075
OverwriteDuplicates => None
10761076
};
1077-
if (duplicate_type != NoError) {
1077+
if duplicate_type != NoError {
10781078
// Return an error here by looking up the namespace that
10791079
// had the duplicate.
10801080
let ns = ns.unwrap();

src/librustc/middle/trans/adt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ pub fn trans_field_ptr(bcx: &Block, r: &Repr, val: ValueRef, discr: Disr,
666666
}
667667
NullablePointer{ nonnull: ref nonnull, nullfields: ref nullfields,
668668
nndiscr, .. } => {
669-
if (discr == nndiscr) {
669+
if discr == nndiscr {
670670
struct_field_ptr(bcx, nonnull, val, ix, false)
671671
} else {
672672
// The unit-like case might have a nonzero number of unit-like fields.
@@ -783,7 +783,7 @@ fn build_const_struct(ccx: &CrateContext, st: &Struct, vals: &[ValueRef])
783783
/*bad*/as u64;
784784
let target_offset = roundup(offset, type_align);
785785
offset = roundup(offset, val_align);
786-
if (offset != target_offset) {
786+
if offset != target_offset {
787787
cfields.push(padding(target_offset - offset));
788788
offset = target_offset;
789789
}

src/librustc/middle/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ pub fn get_item_val(ccx: @CrateContext, id: ast::NodeId) -> ValueRef {
21562156
_ => fail!("get_item_val: weird result in table")
21572157
};
21582158

2159-
match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) {
2159+
match attr::first_attr_value_str_by_name(i.attrs, "link_section") {
21602160
Some(sect) => unsafe {
21612161
sect.with_c_str(|buf| {
21622162
llvm::LLVMSetSection(v, buf);

src/librustc/middle/trans/tvec.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ pub fn iter_vec_raw<'r,
686686
let fcx = bcx.fcx;
687687

688688
let vt = vec_types(bcx, vec_ty);
689-
if (vt.llunit_alloc_size == 0) {
689+
if vt.llunit_alloc_size == 0 {
690690
// Special-case vectors with elements of size 0 so they don't go out of bounds (#9890)
691691
iter_vec_loop(bcx, data_ptr, &vt, fill, f)
692692
} else {
@@ -740,4 +740,3 @@ pub fn iter_vec_unboxed<'r,
740740
let dataptr = get_dataptr(bcx, body_ptr);
741741
return iter_vec_raw(bcx, dataptr, vec_ty, fill, f);
742742
}
743-

src/librustc/middle/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2883,7 +2883,7 @@ pub fn adjust_ty(cx: ctxt,
28832883
AutoDerefRef(ref adj) => {
28842884
let mut adjusted_ty = unadjusted_ty;
28852885

2886-
if (!ty::type_is_error(adjusted_ty)) {
2886+
if !ty::type_is_error(adjusted_ty) {
28872887
for i in range(0, adj.autoderefs) {
28882888
match ty::deref(adjusted_ty, true) {
28892889
Some(mt) => { adjusted_ty = mt.ty; }

0 commit comments

Comments
 (0)