Skip to content

Commit d969988

Browse files
committed
Remove uncessary parens in closure body with unused lint
1 parent 414482f commit d969988

File tree

15 files changed

+160
-27
lines changed

15 files changed

+160
-27
lines changed

compiler/rustc_errors/src/emitter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3511,7 +3511,7 @@ pub fn is_case_difference(sm: &SourceMap, suggested: &str, sp: Span) -> bool {
35113511
// All the chars that differ in capitalization are confusable (above):
35123512
let confusable = iter::zip(found.chars(), suggested.chars())
35133513
.filter(|(f, s)| f != s)
3514-
.all(|(f, s)| (ascii_confusables.contains(&f) || ascii_confusables.contains(&s)));
3514+
.all(|(f, s)| ascii_confusables.contains(&f) || ascii_confusables.contains(&s));
35153515
confusable && found.to_lowercase() == suggested.to_lowercase()
35163516
// FIXME: We sometimes suggest the same thing we already have, which is a
35173517
// bug, but be defensive against that here.

compiler/rustc_lint/src/unused.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::iter;
22

33
use rustc_ast as ast;
44
use rustc_ast::util::{classify, parser};
5-
use rustc_ast::{ExprKind, StmtKind};
5+
use rustc_ast::{ExprKind, FnRetTy, StmtKind};
66
use rustc_errors::{MultiSpan, pluralize};
77
use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::def_id::DefId;
@@ -593,6 +593,7 @@ enum UnusedDelimsCtx {
593593
AnonConst,
594594
MatchArmExpr,
595595
IndexExpr,
596+
ClosureBody,
596597
}
597598

598599
impl From<UnusedDelimsCtx> for &'static str {
@@ -614,6 +615,7 @@ impl From<UnusedDelimsCtx> for &'static str {
614615
UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression",
615616
UnusedDelimsCtx::MatchArmExpr => "match arm expression",
616617
UnusedDelimsCtx::IndexExpr => "index expression",
618+
UnusedDelimsCtx::ClosureBody => "closure body",
617619
}
618620
}
619621
}
@@ -909,6 +911,11 @@ trait UnusedDelimLint {
909911
let (args_to_check, ctx) = match *call_or_other {
910912
Call(_, ref args) => (&args[..], UnusedDelimsCtx::FunctionArg),
911913
MethodCall(ref call) => (&call.args[..], UnusedDelimsCtx::MethodArg),
914+
Closure(ref closure)
915+
if matches!(closure.fn_decl.output, FnRetTy::Default(_)) =>
916+
{
917+
(&[closure.body.clone()][..], UnusedDelimsCtx::ClosureBody)
918+
}
912919
// actual catch-all arm
913920
_ => {
914921
return;
@@ -1392,6 +1399,7 @@ impl UnusedDelimLint for UnusedBraces {
13921399
&& (ctx != UnusedDelimsCtx::AnonConst
13931400
|| (matches!(expr.kind, ast::ExprKind::Lit(_))
13941401
&& !expr.span.from_expansion()))
1402+
&& ctx != UnusedDelimsCtx::ClosureBody
13951403
&& !cx.sess().source_map().is_multiline(value.span)
13961404
&& value.attrs.is_empty()
13971405
&& !value.span.from_expansion()

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2214,7 +2214,7 @@ impl<'a> Parser<'a> {
22142214

22152215
if self.look_ahead(1, |t| *t == token::Bang) && self.look_ahead(2, |t| t.is_ident()) {
22162216
return IsMacroRulesItem::Yes { has_bang: true };
2217-
} else if self.look_ahead(1, |t| (t.is_ident())) {
2217+
} else if self.look_ahead(1, |t| t.is_ident()) {
22182218
// macro_rules foo
22192219
self.dcx().emit_err(errors::MacroRulesMissingBang {
22202220
span: macro_rules_span,

compiler/rustc_resolve/src/late/diagnostics.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
327327
let module_did = mod_prefix.as_ref().and_then(Res::mod_def_id);
328328

329329
let mod_prefix =
330-
mod_prefix.map_or_else(String::new, |res| (format!("{} ", res.descr())));
331-
330+
mod_prefix.map_or_else(String::new, |res| format!("{} ", res.descr()));
332331
(mod_prefix, format!("`{}`", Segment::names_to_string(mod_path)), module_did, None)
333332
};
334333

library/core/src/unicode/unicode_data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ unsafe fn skip_search<const SOR: usize, const OFFSETS: usize>(
8282
let needle = needle as u32;
8383

8484
let last_idx =
85-
match short_offset_runs.binary_search_by_key(&(needle << 11), |header| (header.0 << 11)) {
85+
match short_offset_runs.binary_search_by_key(&(needle << 11), |header| header.0 << 11) {
8686
Ok(idx) => idx + 1,
8787
Err(idx) => idx,
8888
};

src/etc/test-float-parse/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ fn launch_tests(tests: &mut [TestInfo], cfg: &Config) -> Duration {
333333
for test in tests.iter_mut() {
334334
test.progress = Some(ui::Progress::new(test, &mut all_progress_bars));
335335
ui::set_panic_hook(&all_progress_bars);
336-
((test.launch)(test, cfg));
336+
(test.launch)(test, cfg);
337337
}
338338

339339
start.elapsed()

src/tools/clippy/clippy_lints/src/unused_async.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl<'tcx> LateLintPass<'tcx> for UnusedAsync {
165165
let iter = self
166166
.unused_async_fns
167167
.iter()
168-
.filter(|UnusedAsyncFn { def_id, .. }| (!self.async_fns_as_value.contains(def_id)));
168+
.filter(|UnusedAsyncFn { def_id, .. }| !self.async_fns_as_value.contains(def_id));
169169

170170
for fun in iter {
171171
span_lint_hir_and_then(

src/tools/clippy/clippy_utils/src/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ impl AdtVariantInfo {
947947
.enumerate()
948948
.map(|(i, f)| (i, approx_ty_size(cx, f.ty(cx.tcx, subst))))
949949
.collect::<Vec<_>>();
950-
fields_size.sort_by(|(_, a_size), (_, b_size)| (a_size.cmp(b_size)));
950+
fields_size.sort_by(|(_, a_size), (_, b_size)| a_size.cmp(b_size));
951951

952952
Self {
953953
ind: i,
@@ -956,7 +956,7 @@ impl AdtVariantInfo {
956956
}
957957
})
958958
.collect::<Vec<_>>();
959-
variants_size.sort_by(|a, b| (b.size.cmp(&a.size)));
959+
variants_size.sort_by(|a, b| b.size.cmp(&a.size));
960960
variants_size
961961
}
962962
}

src/tools/compiletest/src/runtest.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,12 @@ impl<'test> TestCx<'test> {
16741674
// Allow tests to use internal features.
16751675
rustc.args(&["-A", "internal_features"]);
16761676

1677+
// Allow tests to have unused parens and braces.
1678+
// Add #![deny(unused_parens, unused_braces)] to the test file if you want to
1679+
// test that these lints are working.
1680+
rustc.args(&["-A", "unused_parens"]);
1681+
rustc.args(&["-A", "unused_braces"]);
1682+
16771683
if self.props.force_host {
16781684
self.maybe_add_external_args(&mut rustc, &self.config.host_rustcflags);
16791685
if !is_rustdoc {

src/tools/unicode-table-generator/src/range_search.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ unsafe fn skip_search<const SOR: usize, const OFFSETS: usize>(
8080
let needle = needle as u32;
8181

8282
let last_idx =
83-
match short_offset_runs.binary_search_by_key(&(needle << 11), |header| (header.0 << 11)) {
83+
match short_offset_runs.binary_search_by_key(&(needle << 11), |header| header.0 << 11) {
8484
Ok(idx) => idx + 1,
8585
Err(idx) => idx,
8686
};

tests/ui/async-await/issues/issue-54752-async-block.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@
44
//@ pp-exact
55

66
fn main() { let _a = (async { }); }
7-
//~^ WARNING unnecessary parentheses around assigned value

tests/ui/async-await/issues/issue-54752-async-block.stderr

-15
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//@ run-rustfix
2+
// ignore-tidy-linelength
3+
#![deny(unused_parens)]
4+
#![deny(unused_braces)]
5+
6+
fn long_expr_that_does_not_require_braces_long_expr_that_does_not_require_braces_long_expr_that_does_not_require_braces()
7+
{}
8+
9+
fn func(f: impl FnOnce()) {
10+
f()
11+
}
12+
13+
pub fn main() {
14+
let _closure = |x: i32, y: i32| { x * (x + (y * 2)) };
15+
let _ = || 0 == 0; //~ ERROR unnecessary parentheses around closure body
16+
let _ = (0..).find(|n| n % 2 == 0); //~ ERROR unnecessary parentheses around closure body
17+
let _ = (0..).find(|n| {n % 2 == 0});
18+
19+
// multiple lines of code will not lint with braces
20+
let _ = (0..).find(|n| {
21+
n % 2 == 0
22+
});
23+
24+
// multiple lines of code will lint with parentheses
25+
let _ = (0..).find(|n| n % 2 == 0);
26+
27+
let _ = || {
28+
_ = 0;
29+
0 == 0 //~ ERROR unnecessary parentheses around block return value
30+
};
31+
32+
// long expressions will not lint with braces
33+
func(|| {
34+
long_expr_that_does_not_require_braces_long_expr_that_does_not_require_braces_long_expr_that_does_not_require_braces()
35+
})
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//@ run-rustfix
2+
// ignore-tidy-linelength
3+
#![deny(unused_parens)]
4+
#![deny(unused_braces)]
5+
6+
fn long_expr_that_does_not_require_braces_long_expr_that_does_not_require_braces_long_expr_that_does_not_require_braces()
7+
{}
8+
9+
fn func(f: impl FnOnce()) {
10+
f()
11+
}
12+
13+
pub fn main() {
14+
let _closure = |x: i32, y: i32| { x * (x + (y * 2)) };
15+
let _ = || (0 == 0); //~ ERROR unnecessary parentheses around closure body
16+
let _ = (0..).find(|n| (n % 2 == 0)); //~ ERROR unnecessary parentheses around closure body
17+
let _ = (0..).find(|n| {n % 2 == 0});
18+
19+
// multiple lines of code will not lint with braces
20+
let _ = (0..).find(|n| {
21+
n % 2 == 0
22+
});
23+
24+
// multiple lines of code will lint with parentheses
25+
let _ = (0..).find(|n| ( //~ ERROR unnecessary parentheses around closure body
26+
n % 2 == 0
27+
));
28+
29+
let _ = || {
30+
_ = 0;
31+
(0 == 0) //~ ERROR unnecessary parentheses around block return value
32+
};
33+
34+
// long expressions will not lint with braces
35+
func(|| {
36+
long_expr_that_does_not_require_braces_long_expr_that_does_not_require_braces_long_expr_that_does_not_require_braces()
37+
})
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
error: unnecessary parentheses around closure body
2+
--> $DIR/closure-body-issue-136741.rs:15:16
3+
|
4+
LL | let _ = || (0 == 0);
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/closure-body-issue-136741.rs:3:9
9+
|
10+
LL | #![deny(unused_parens)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these parentheses
13+
|
14+
LL - let _ = || (0 == 0);
15+
LL + let _ = || 0 == 0;
16+
|
17+
18+
error: unnecessary parentheses around closure body
19+
--> $DIR/closure-body-issue-136741.rs:16:28
20+
|
21+
LL | let _ = (0..).find(|n| (n % 2 == 0));
22+
| ^ ^
23+
|
24+
help: remove these parentheses
25+
|
26+
LL - let _ = (0..).find(|n| (n % 2 == 0));
27+
LL + let _ = (0..).find(|n| n % 2 == 0);
28+
|
29+
30+
error: unnecessary parentheses around closure body
31+
--> $DIR/closure-body-issue-136741.rs:25:28
32+
|
33+
LL | let _ = (0..).find(|n| (
34+
| _____________________________^
35+
LL | | n % 2 == 0
36+
| | ________^__________^
37+
| ||________|
38+
| |
39+
LL | | ));
40+
| |_____^
41+
|
42+
help: remove these parentheses
43+
|
44+
LL - let _ = (0..).find(|n| (
45+
LL - n % 2 == 0
46+
LL + let _ = (0..).find(|n| n % 2 == 0);
47+
|
48+
49+
error: unnecessary parentheses around block return value
50+
--> $DIR/closure-body-issue-136741.rs:31:9
51+
|
52+
LL | (0 == 0)
53+
| ^ ^
54+
|
55+
help: remove these parentheses
56+
|
57+
LL - (0 == 0)
58+
LL + 0 == 0
59+
|
60+
61+
error: aborting due to 4 previous errors
62+

0 commit comments

Comments
 (0)