Skip to content

Commit 2a91e96

Browse files
committed
Add checking for unnecessary delims in closure body
1 parent 9e66c19 commit 2a91e96

File tree

16 files changed

+124
-29
lines changed

16 files changed

+124
-29
lines changed

compiler/rustc_errors/src/emitter.rs

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

compiler/rustc_lint/src/unused.rs

+15-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,18 @@ 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+
// skip `#[core::contracts::requires(...)]` and `#[core::contracts::ensures(...)]` which generate closure
917+
&& !cx
918+
.sess()
919+
.source_map()
920+
.span_to_snippet(closure.fn_decl_span)
921+
.unwrap_or_default()
922+
.contains("core::contracts") =>
923+
{
924+
(&[closure.body.clone()][..], UnusedDelimsCtx::ClosureBody)
925+
}
912926
// actual catch-all arm
913927
_ => {
914928
return;

compiler/rustc_mir_transform/src/coverage/counters/node_flow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl<'a, Node: Idx> SpantreeBuilder<'a, Node> {
261261
debug_assert!(
262262
span_edges
263263
.iter_enumerated()
264-
.all(|(node, span_edge)| { span_edge.is_some() <= self.is_supernode(node) }),
264+
.all(|(node, span_edge)| span_edge.is_some() <= self.is_supernode(node)),
265265
"only supernodes can have a span edge",
266266
);
267267
debug_assert!(

compiler/rustc_mir_transform/src/coverage/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn create_mappings(extracted_mappings: &ExtractedMappings) -> Vec<Mapping> {
159159
condition_info: _,
160160
true_index: _,
161161
false_index: _,
162-
}| { Mapping { kind: MappingKind::Branch { true_bcb, false_bcb }, span } },
162+
}| Mapping { kind: MappingKind::Branch { true_bcb, false_bcb }, span },
163163
));
164164

165165
for (decision, branches) in mcdc_mappings {

compiler/rustc_parse/src/parser/item.rs

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

22022202
if self.look_ahead(1, |t| *t == token::Bang) && self.look_ahead(2, |t| t.is_ident()) {
22032203
return IsMacroRulesItem::Yes { has_bang: true };
2204-
} else if self.look_ahead(1, |t| (t.is_ident())) {
2204+
} else if self.look_ahead(1, |t| t.is_ident()) {
22052205
// macro_rules foo
22062206
self.dcx().emit_err(errors::MacroRulesMissingBang {
22072207
span: macro_rules_span,

compiler/rustc_resolve/src/late/diagnostics.rs

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

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

src/librustdoc/html/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ impl clean::FnDecl {
13071307
fmt::from_fn(move |f| {
13081308
// First, generate the text form of the declaration, with no line wrapping, and count the bytes.
13091309
let mut counter = WriteCounter(0);
1310-
write!(&mut counter, "{:#}", fmt::from_fn(|f| { self.inner_full_print(None, f, cx) }))
1310+
write!(&mut counter, "{:#}", fmt::from_fn(|f| self.inner_full_print(None, f, cx)))
13111311
.unwrap();
13121312
// If the text form was over 80 characters wide, we will line-wrap our output.
13131313
let line_wrapping_indent =

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/higher.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<'hir> IfLetOrMatch<'hir> {
172172
if_then,
173173
if_else,
174174
let_span,
175-
}| { Self::IfLet(let_expr, let_pat, if_then, if_else, let_span) },
175+
}| Self::IfLet(let_expr, let_pat, if_then, if_else, let_span),
176176
),
177177
}
178178
}

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/miri/src/concurrency/thread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ impl<'tcx> ThreadManager<'tcx> {
641641
assert!(
642642
self.threads
643643
.iter()
644-
.all(|thread| { !thread.state.is_blocked_on(BlockReason::Join(joined_thread_id)) }),
644+
.all(|thread| !thread.state.is_blocked_on(BlockReason::Join(joined_thread_id))),
645645
"this thread already has threads waiting for its termination"
646646
);
647647

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,13 @@
1+
//@ run-rustfix
2+
#![deny(unused_parens)]
3+
#![deny(unused_braces)]
4+
pub fn main() {
5+
let _closure = |x: i32, y: i32| x * (x + (y * 2)); //~ ERROR unnecessary braces around closure body
6+
let _ = || 0 == 0; //~ ERROR unnecessary parentheses around closure body
7+
let _ = (0..).find(|n| n % 2 == 0); //~ ERROR unnecessary parentheses around closure body
8+
let _ = (0..).find(|n| n % 2 == 0); //~ ERROR unnecessary braces around closure body
9+
let _ = || {
10+
_ = 0;
11+
0 == 0 //~ ERROR unnecessary parentheses around block return value
12+
};
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ run-rustfix
2+
#![deny(unused_parens)]
3+
#![deny(unused_braces)]
4+
pub fn main() {
5+
let _closure = |x: i32, y: i32| { x * (x + (y * 2)) }; //~ ERROR unnecessary braces around closure body
6+
let _ = || (0 == 0); //~ ERROR unnecessary parentheses around closure body
7+
let _ = (0..).find(|n| (n % 2 == 0)); //~ ERROR unnecessary parentheses around closure body
8+
let _ = (0..).find(|n| {n % 2 == 0}); //~ ERROR unnecessary braces around closure body
9+
let _ = || {
10+
_ = 0;
11+
(0 == 0) //~ ERROR unnecessary parentheses around block return value
12+
};
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
error: unnecessary braces around closure body
2+
--> $DIR/closure-body-issue-136741.rs:5:37
3+
|
4+
LL | let _closure = |x: i32, y: i32| { x * (x + (y * 2)) };
5+
| ^^ ^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/closure-body-issue-136741.rs:3:9
9+
|
10+
LL | #![deny(unused_braces)]
11+
| ^^^^^^^^^^^^^
12+
help: remove these braces
13+
|
14+
LL - let _closure = |x: i32, y: i32| { x * (x + (y * 2)) };
15+
LL + let _closure = |x: i32, y: i32| x * (x + (y * 2));
16+
|
17+
18+
error: unnecessary parentheses around closure body
19+
--> $DIR/closure-body-issue-136741.rs:6:16
20+
|
21+
LL | let _ = || (0 == 0);
22+
| ^ ^
23+
|
24+
note: the lint level is defined here
25+
--> $DIR/closure-body-issue-136741.rs:2:9
26+
|
27+
LL | #![deny(unused_parens)]
28+
| ^^^^^^^^^^^^^
29+
help: remove these parentheses
30+
|
31+
LL - let _ = || (0 == 0);
32+
LL + let _ = || 0 == 0;
33+
|
34+
35+
error: unnecessary parentheses around closure body
36+
--> $DIR/closure-body-issue-136741.rs:7:28
37+
|
38+
LL | let _ = (0..).find(|n| (n % 2 == 0));
39+
| ^ ^
40+
|
41+
help: remove these parentheses
42+
|
43+
LL - let _ = (0..).find(|n| (n % 2 == 0));
44+
LL + let _ = (0..).find(|n| n % 2 == 0);
45+
|
46+
47+
error: unnecessary braces around closure body
48+
--> $DIR/closure-body-issue-136741.rs:8:28
49+
|
50+
LL | let _ = (0..).find(|n| {n % 2 == 0});
51+
| ^ ^
52+
|
53+
help: remove these braces
54+
|
55+
LL - let _ = (0..).find(|n| {n % 2 == 0});
56+
LL + let _ = (0..).find(|n| n % 2 == 0);
57+
|
58+
59+
error: unnecessary parentheses around block return value
60+
--> $DIR/closure-body-issue-136741.rs:11:9
61+
|
62+
LL | (0 == 0)
63+
| ^ ^
64+
|
65+
help: remove these parentheses
66+
|
67+
LL - (0 == 0)
68+
LL + 0 == 0
69+
|
70+
71+
error: aborting due to 5 previous errors
72+

0 commit comments

Comments
 (0)