Skip to content

Commit eb5ba4d

Browse files
committed
auto merge of #12366 : aepsil0n/rust/feature/unnecessary_parens_around_assigned_values, r=alexcrichton
Fixes #12350. Parentheses around assignment statements such as ```rust let mut a = (0); a = (1); a += (2); ``` are not necessary and therefore an unnecessary_parens warning is raised when statements like this occur. NOTE: In `let` declarations this does not work as intended. Is it possible that they do not count as assignment expressions (`ExprAssign`)? (edit: this is fixed by now) Furthermore, there are some cases that I fixed in the rest of the code, where parentheses could potentially enhance readability. Compare these lines: ```rust a = b == c; a = (b == c); ``` Thus, after having worked on this I'm not entirely sure, whether we should go through with this patch or not. Probably a matter of debate. ;)
2 parents 87e3b5f + 9982de6 commit eb5ba4d

File tree

15 files changed

+51
-34
lines changed

15 files changed

+51
-34
lines changed

src/librustc/metadata/loader.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -407,11 +407,8 @@ fn get_metadata_section_imp(os: Os, filename: &Path) -> Option<MetadataBlob> {
407407
debug!("checking {} bytes of metadata-version stamp",
408408
vlen);
409409
let minsz = cmp::min(vlen, csz);
410-
let mut version_ok = false;
411-
vec::raw::buf_as_slice(cvbuf, minsz, |buf0| {
412-
version_ok = (buf0 ==
413-
encoder::metadata_encoding_version);
414-
});
410+
let version_ok = vec::raw::buf_as_slice(cvbuf, minsz,
411+
|buf0| buf0 == encoder::metadata_encoding_version);
415412
if !version_ok { return None; }
416413

417414
let cvbuf1 = cvbuf.offset(vlen as int);

src/librustc/middle/borrowck/gather_loans/lifetime.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,10 @@ impl<'a> GuaranteeLifetimeContext<'a> {
9595
let base_scope = self.scope(base);
9696

9797
// L-Deref-Managed-Imm-User-Root
98-
let omit_root = (
98+
let omit_root =
9999
self.bccx.is_subregion_of(self.loan_region, base_scope) &&
100100
self.is_rvalue_or_immutable(base) &&
101-
!self.is_moved(base)
102-
);
101+
!self.is_moved(base);
103102

104103
if !omit_root {
105104
// L-Deref-Managed-Imm-Compiler-Root

src/librustc/middle/dataflow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ fn bitwise(out_vec: &mut [uint], in_vec: &[uint], op: |uint, uint| -> uint)
891891
let old_val = *out_elt;
892892
let new_val = op(old_val, *in_elt);
893893
*out_elt = new_val;
894-
changed |= (old_val != new_val);
894+
changed |= old_val != new_val;
895895
}
896896
changed
897897
}

src/librustc/middle/lint.rs

+29-9
Original file line numberDiff line numberDiff line change
@@ -1167,22 +1167,41 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
11671167
}
11681168
}
11691169

1170-
fn check_unnecessary_parens(cx: &Context, e: &ast::Expr) {
1170+
fn check_unnecessary_parens_core(cx: &Context, value: &ast::Expr, msg: &str) {
1171+
match value.node {
1172+
ast::ExprParen(_) => {
1173+
cx.span_lint(UnnecessaryParens, value.span,
1174+
format!("unnecessary parentheses around {}", msg))
1175+
}
1176+
_ => {}
1177+
}
1178+
}
1179+
1180+
fn check_unnecessary_parens_expr(cx: &Context, e: &ast::Expr) {
11711181
let (value, msg) = match e.node {
11721182
ast::ExprIf(cond, _, _) => (cond, "`if` condition"),
11731183
ast::ExprWhile(cond, _) => (cond, "`while` condition"),
11741184
ast::ExprMatch(head, _) => (head, "`match` head expression"),
11751185
ast::ExprRet(Some(value)) => (value, "`return` value"),
1186+
ast::ExprAssign(_, value) => (value, "assigned value"),
1187+
ast::ExprAssignOp(_, _, _, value) => (value, "assigned value"),
11761188
_ => return
11771189
};
1190+
check_unnecessary_parens_core(cx, value, msg);
1191+
}
11781192

1179-
match value.node {
1180-
ast::ExprParen(_) => {
1181-
cx.span_lint(UnnecessaryParens, value.span,
1182-
format!("unnecessary parentheses around {}", msg))
1183-
}
1184-
_ => {}
1185-
}
1193+
fn check_unnecessary_parens_stmt(cx: &Context, s: &ast::Stmt) {
1194+
let (value, msg) = match s.node {
1195+
ast::StmtDecl(decl, _) => match decl.node {
1196+
ast::DeclLocal(local) => match local.init {
1197+
Some(value) => (value, "assigned value"),
1198+
None => return
1199+
},
1200+
_ => return
1201+
},
1202+
_ => return
1203+
};
1204+
check_unnecessary_parens_core(cx, value, msg);
11861205
}
11871206

11881207
fn check_unused_unsafe(cx: &Context, e: &ast::Expr) {
@@ -1534,7 +1553,7 @@ impl<'a> Visitor<()> for Context<'a> {
15341553

15351554
check_while_true_expr(self, e);
15361555
check_stability(self, e);
1537-
check_unnecessary_parens(self, e);
1556+
check_unnecessary_parens_expr(self, e);
15381557
check_unused_unsafe(self, e);
15391558
check_unsafe_block(self, e);
15401559
check_unnecessary_allocation(self, e);
@@ -1549,6 +1568,7 @@ impl<'a> Visitor<()> for Context<'a> {
15491568
fn visit_stmt(&mut self, s: &ast::Stmt, _: ()) {
15501569
check_path_statement(self, s);
15511570
check_unused_result(self, s);
1571+
check_unnecessary_parens_stmt(self, s);
15521572

15531573
visit::walk_stmt(self, s, ());
15541574
}

src/librustc/middle/trans/monomorphize.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,8 @@ pub fn monomorphic_fn(ccx: @CrateContext,
143143
// This is a bit unfortunate.
144144

145145
let idx = psubsts.tys.len() - num_method_ty_params;
146-
let substs =
147-
(psubsts.tys.slice(0, idx) +
148-
&[psubsts.self_ty.unwrap()] +
149-
psubsts.tys.tailn(idx));
146+
let substs = psubsts.tys.slice(0, idx) +
147+
&[psubsts.self_ty.unwrap()] + psubsts.tys.tailn(idx);
150148
debug!("static default: changed substitution to {}",
151149
substs.repr(ccx.tcx));
152150

src/librustc/middle/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2293,8 +2293,8 @@ pub fn type_contents(cx: ctxt, ty: t) -> TypeContents {
22932293
bounds: BuiltinBounds)
22942294
-> TypeContents {
22952295
// These are the type contents of the (opaque) interior
2296-
let contents = (TC::ReachesMutable.when(mutbl == ast::MutMutable) |
2297-
kind_bounds_to_contents(cx, bounds, []));
2296+
let contents = TC::ReachesMutable.when(mutbl == ast::MutMutable) |
2297+
kind_bounds_to_contents(cx, bounds, []);
22982298

22992299
match store {
23002300
UniqTraitStore => {

src/libserialize/ebml.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ pub mod writer {
674674
let last_size_pos = self.size_positions.pop().unwrap();
675675
let cur_pos = try!(self.writer.tell());
676676
try!(self.writer.seek(last_size_pos as i64, io::SeekSet));
677-
let size = (cur_pos as uint - last_size_pos - 4);
677+
let size = cur_pos as uint - last_size_pos - 4;
678678
write_sized_vuint(self.writer, size, 4u);
679679
try!(self.writer.seek(cur_pos as i64, io::SeekSet));
680680

src/libstd/os.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ pub mod win32 {
119119
} else if k == n &&
120120
libc::GetLastError() ==
121121
libc::ERROR_INSUFFICIENT_BUFFER as DWORD {
122-
n *= (2 as DWORD);
122+
n *= 2 as DWORD;
123123
} else if k >= n {
124124
n = k;
125125
} else {
@@ -225,7 +225,7 @@ pub fn env_as_bytes() -> ~[(~[u8],~[u8])] {
225225
for p in input.iter() {
226226
let vs: ~[&[u8]] = p.splitn(1, |b| *b == '=' as u8).collect();
227227
let key = vs[0].to_owned();
228-
let val = (if vs.len() < 2 { ~[] } else { vs[1].to_owned() });
228+
let val = if vs.len() < 2 { ~[] } else { vs[1].to_owned() };
229229
pairs.push((key, val));
230230
}
231231
pairs

src/libsyntax/abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl AbiSet {
202202
}
203203

204204
pub fn add(&mut self, abi: Abi) {
205-
self.bits |= (1 << abi.index());
205+
self.bits |= 1 << abi.index();
206206
}
207207

208208
pub fn each(&self, op: |abi: Abi| -> bool) -> bool {

src/libsyntax/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,6 @@ mod test {
12241224
},
12251225
};
12261226
// doesn't matter which encoder we use....
1227-
let _f = (&e as &serialize::Encodable<json::Encoder>);
1227+
let _f = &e as &serialize::Encodable<json::Encoder>;
12281228
}
12291229
}

src/libsyntax/diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ fn highlight_lines(cm: &codemap::CodeMap,
329329
for _ in range(0, skip) { s.push_char(' '); }
330330
let orig = fm.get_line(lines.lines[0] as int);
331331
for pos in range(0u, left-skip) {
332-
let curChar = (orig[pos] as char);
332+
let curChar = orig[pos] as char;
333333
// Whenever a tab occurs on the previous line, we insert one on
334334
// the error-point-squiggly-line as well (instead of a space).
335335
// That way the squiggly line will usually appear in the correct

src/libterm/terminfo/parm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables)
259259
' ' => flags.space = true,
260260
'.' => fstate = FormatStatePrecision,
261261
'0'..'9' => {
262-
flags.width = (cur as uint - '0' as uint);
262+
flags.width = cur as uint - '0' as uint;
263263
fstate = FormatStateWidth;
264264
}
265265
_ => unreachable!()
@@ -359,7 +359,7 @@ pub fn expand(cap: &[u8], params: &[Param], vars: &mut Variables)
359359
flags.space = true;
360360
}
361361
(FormatStateFlags,'0'..'9') => {
362-
flags.width = (cur as uint - '0' as uint);
362+
flags.width = cur as uint - '0' as uint;
363363
*fstate = FormatStateWidth;
364364
}
365365
(FormatStateFlags,'.') => {

src/libtest/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1047,7 +1047,7 @@ impl MetricMap {
10471047
let r = match selfmap.find(k) {
10481048
None => MetricRemoved,
10491049
Some(v) => {
1050-
let delta = (v.value - vold.value);
1050+
let delta = v.value - vold.value;
10511051
let noise = match noise_pct {
10521052
None => f64::max(vold.noise.abs(), v.noise.abs()),
10531053
Some(pct) => vold.value * pct / 100.0

src/libuuid/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl Uuid {
296296
///
297297
/// This represents the algorithm used to generate the contents
298298
pub fn get_version(&self) -> Option<UuidVersion> {
299-
let v = (self.bytes[6] >> 4);
299+
let v = self.bytes[6] >> 4;
300300
match v {
301301
1 => Some(Version1Mac),
302302
2 => Some(Version2Dce),

src/test/compile-fail/lint-unnecessary-parens.rs

+3
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ fn main() {
2222
match (true) { //~ ERROR unnecessary parentheses around `match` head expression
2323
_ => {}
2424
}
25+
let mut _a = (0); //~ ERROR unnecessary parentheses around assigned value
26+
_a = (0); //~ ERROR unnecessary parentheses around assigned value
27+
_a += (1); //~ ERROR unnecessary parentheses around assigned value
2528
}

0 commit comments

Comments
 (0)