Skip to content

Commit 37cd2d8

Browse files
committed
Add checking for unnecessary delims in closure body
1 parent 421facc commit 37cd2d8

File tree

16 files changed

+124
-29
lines changed

16 files changed

+124
-29
lines changed

compiler/rustc_errors/src/emitter.rs

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

compiler/rustc_lint/src/unused.rs

Lines changed: 15 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ impl<'a> Parser<'a> {
21862186

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

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 1 addition & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ impl clean::FnDecl {
13421342
fmt::from_fn(move |f| {
13431343
// First, generate the text form of the declaration, with no line wrapping, and count the bytes.
13441344
let mut counter = WriteCounter(0);
1345-
write!(&mut counter, "{:#}", fmt::from_fn(|f| { self.inner_full_print(None, f, cx) }))
1345+
write!(&mut counter, "{:#}", fmt::from_fn(|f| self.inner_full_print(None, f, cx)))
13461346
.unwrap();
13471347
// If the text form was over 80 characters wide, we will line-wrap our output.
13481348
let line_wrapping_indent =

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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
}

0 commit comments

Comments
 (0)