Skip to content

Commit 9ec13a6

Browse files
committed
Add checking for unnecessary delims in closure body
1 parent 3a34ad7 commit 9ec13a6

File tree

16 files changed

+124
-29
lines changed

16 files changed

+124
-29
lines changed

Diff for: compiler/rustc_errors/src/emitter.rs

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

Diff for: compiler/rustc_lint/src/unused.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::ControlFlow;
33

44
use rustc_ast as ast;
55
use rustc_ast::util::{classify, parser};
6-
use rustc_ast::{ExprKind, StmtKind};
6+
use rustc_ast::{ExprKind, FnRetTy, StmtKind};
77
use rustc_errors::{MultiSpan, pluralize};
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::def_id::DefId;
@@ -594,6 +594,7 @@ enum UnusedDelimsCtx {
594594
AnonConst,
595595
MatchArmExpr,
596596
IndexExpr,
597+
ClosureBody,
597598
}
598599

599600
impl From<UnusedDelimsCtx> for &'static str {
@@ -615,6 +616,7 @@ impl From<UnusedDelimsCtx> for &'static str {
615616
UnusedDelimsCtx::ArrayLenExpr | UnusedDelimsCtx::AnonConst => "const expression",
616617
UnusedDelimsCtx::MatchArmExpr => "match arm expression",
617618
UnusedDelimsCtx::IndexExpr => "index expression",
619+
UnusedDelimsCtx::ClosureBody => "closure body",
618620
}
619621
}
620622
}
@@ -933,6 +935,18 @@ trait UnusedDelimLint {
933935
let (args_to_check, ctx) = match *call_or_other {
934936
Call(_, ref args) => (&args[..], UnusedDelimsCtx::FunctionArg),
935937
MethodCall(ref call) => (&call.args[..], UnusedDelimsCtx::MethodArg),
938+
Closure(ref closure)
939+
if matches!(closure.fn_decl.output, FnRetTy::Default(_))
940+
// skip `#[core::contracts::requires(...)]` and `#[core::contracts::ensures(...)]` which generate closure
941+
&& !cx
942+
.sess()
943+
.source_map()
944+
.span_to_snippet(closure.fn_decl_span)
945+
.unwrap_or_default()
946+
.contains("core::contracts") =>
947+
{
948+
(&[closure.body.clone()][..], UnusedDelimsCtx::ClosureBody)
949+
}
936950
// actual catch-all arm
937951
_ => {
938952
return;

Diff for: 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!(

Diff for: compiler/rustc_mir_transform/src/coverage/mod.rs

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

166166
for (decision, branches) in mcdc_mappings {

Diff for: compiler/rustc_parse/src/parser/item.rs

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

21902190
if self.look_ahead(1, |t| *t == token::Not) && self.look_ahead(2, |t| t.is_ident()) {
21912191
return IsMacroRulesItem::Yes { has_bang: true };
2192-
} else if self.look_ahead(1, |t| (t.is_ident())) {
2192+
} else if self.look_ahead(1, |t| t.is_ident()) {
21932193
// macro_rules foo
21942194
self.dcx().emit_err(errors::MacroRulesMissingBang {
21952195
span: macro_rules_span,

Diff for: 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

Diff for: src/librustdoc/html/format.rs

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

Diff for: 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(

Diff for: 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
}

Diff for: src/tools/clippy/clippy_utils/src/ty/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ impl AdtVariantInfo {
927927
.enumerate()
928928
.map(|(i, f)| (i, approx_ty_size(cx, f.ty(cx.tcx, subst))))
929929
.collect::<Vec<_>>();
930-
fields_size.sort_by(|(_, a_size), (_, b_size)| (a_size.cmp(b_size)));
930+
fields_size.sort_by(|(_, a_size), (_, b_size)| a_size.cmp(b_size));
931931

932932
Self {
933933
ind: i,
@@ -936,7 +936,7 @@ impl AdtVariantInfo {
936936
}
937937
})
938938
.collect::<Vec<_>>();
939-
variants_size.sort_by(|a, b| (b.size.cmp(&a.size)));
939+
variants_size.sort_by(|a, b| b.size.cmp(&a.size));
940940
variants_size
941941
}
942942
}

Diff for: 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

Diff for: 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

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

-15
This file was deleted.

Diff for: tests/ui/lint/unused/closure-body-issue-136741.fixed

+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+
}

Diff for: tests/ui/lint/unused/closure-body-issue-136741.rs

+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+
}
+72
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)