Skip to content

Commit 637d39f

Browse files
committed
Auto merge of rust-lang#13065 - Jarcho:misc_small2, r=xFrednet
Misc refactorings part 2 A couple of theses are a bit more involved. No behaviour changes included in this set. changelog: none
2 parents 1807580 + f493d71 commit 637d39f

13 files changed

+169
-264
lines changed

clippy_lints/src/double_parens.rs

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,29 @@ declare_lint_pass!(DoubleParens => [DOUBLE_PARENS]);
4040

4141
impl EarlyLintPass for DoubleParens {
4242
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
43-
if expr.span.from_expansion() {
44-
return;
45-
}
46-
47-
let msg: &str = "consider removing unnecessary double parentheses";
48-
49-
match expr.kind {
50-
ExprKind::Paren(ref in_paren) => match in_paren.kind {
51-
ExprKind::Paren(_) | ExprKind::Tup(_) => {
52-
span_lint(cx, DOUBLE_PARENS, expr.span, msg);
53-
},
54-
_ => {},
55-
},
56-
ExprKind::Call(_, ref params) => {
57-
if params.len() == 1 {
58-
let param = &params[0];
59-
if let ExprKind::Paren(_) = param.kind {
60-
span_lint(cx, DOUBLE_PARENS, param.span, msg);
61-
}
62-
}
43+
let span = match &expr.kind {
44+
ExprKind::Paren(in_paren) if matches!(in_paren.kind, ExprKind::Paren(_) | ExprKind::Tup(_)) => expr.span,
45+
ExprKind::Call(_, params)
46+
if let [param] = &**params
47+
&& let ExprKind::Paren(_) = param.kind =>
48+
{
49+
param.span
6350
},
64-
ExprKind::MethodCall(ref call) => {
65-
if let [ref arg] = call.args[..] {
66-
if let ExprKind::Paren(_) = arg.kind {
67-
span_lint(cx, DOUBLE_PARENS, arg.span, msg);
68-
}
69-
}
51+
ExprKind::MethodCall(call)
52+
if let [arg] = &*call.args
53+
&& let ExprKind::Paren(_) = arg.kind =>
54+
{
55+
arg.span
7056
},
71-
_ => {},
57+
_ => return,
58+
};
59+
if !expr.span.from_expansion() {
60+
span_lint(
61+
cx,
62+
DOUBLE_PARENS,
63+
span,
64+
"consider removing unnecessary double parentheses",
65+
);
7266
}
7367
}
7468
}

clippy_lints/src/endian_bytes.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,32 +109,27 @@ impl LintKind {
109109

110110
impl LateLintPass<'_> for EndianBytes {
111111
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
112-
if in_external_macro(cx.sess(), expr.span) {
113-
return;
114-
}
115-
116-
if let ExprKind::MethodCall(method_name, receiver, args, ..) = expr.kind
117-
&& args.is_empty()
118-
&& let ty = cx.typeck_results().expr_ty(receiver)
112+
let (prefix, name, ty_expr) = match expr.kind {
113+
ExprKind::MethodCall(method_name, receiver, [], ..) => (Prefix::To, method_name.ident.name, receiver),
114+
ExprKind::Call(function, ..)
115+
if let ExprKind::Path(qpath) = function.kind
116+
&& let Some(def_id) = cx.qpath_res(&qpath, function.hir_id).opt_def_id()
117+
&& let Some(function_name) = cx.get_def_path(def_id).last() =>
118+
{
119+
(Prefix::From, *function_name, expr)
120+
},
121+
_ => return,
122+
};
123+
if !in_external_macro(cx.sess(), expr.span)
124+
&& let ty = cx.typeck_results().expr_ty(ty_expr)
119125
&& ty.is_primitive_ty()
120-
&& maybe_lint_endian_bytes(cx, expr, Prefix::To, method_name.ident.name, ty)
121126
{
122-
return;
123-
}
124-
125-
if let ExprKind::Call(function, ..) = expr.kind
126-
&& let ExprKind::Path(qpath) = function.kind
127-
&& let Some(def_id) = cx.qpath_res(&qpath, function.hir_id).opt_def_id()
128-
&& let Some(function_name) = cx.get_def_path(def_id).last()
129-
&& let ty = cx.typeck_results().expr_ty(expr)
130-
&& ty.is_primitive_ty()
131-
{
132-
maybe_lint_endian_bytes(cx, expr, Prefix::From, *function_name, ty);
127+
maybe_lint_endian_bytes(cx, expr, prefix, name, ty);
133128
}
134129
}
135130
}
136131

137-
fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix, name: Symbol, ty: Ty<'_>) -> bool {
132+
fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix, name: Symbol, ty: Ty<'_>) {
138133
let ne = LintKind::Host.as_name(prefix);
139134
let le = LintKind::Little.as_name(prefix);
140135
let be = LintKind::Big.as_name(prefix);
@@ -143,7 +138,7 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix
143138
name if name == ne => ((&LintKind::Host), [(&LintKind::Little), (&LintKind::Big)]),
144139
name if name == le => ((&LintKind::Little), [(&LintKind::Host), (&LintKind::Big)]),
145140
name if name == be => ((&LintKind::Big), [(&LintKind::Host), (&LintKind::Little)]),
146-
_ => return false,
141+
_ => return,
147142
};
148143

149144
let mut help = None;
@@ -208,6 +203,4 @@ fn maybe_lint_endian_bytes(cx: &LateContext<'_>, expr: &Expr<'_>, prefix: Prefix
208203
}
209204
},
210205
);
211-
212-
true
213206
}

clippy_lints/src/excessive_bools.rs

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -88,77 +88,67 @@ pub struct ExcessiveBools {
8888
max_fn_params_bools: u64,
8989
}
9090

91-
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
92-
enum Kind {
93-
Struct,
94-
Fn,
95-
}
96-
9791
impl ExcessiveBools {
9892
pub fn new(conf: &'static Conf) -> Self {
9993
Self {
10094
max_struct_bools: conf.max_struct_bools,
10195
max_fn_params_bools: conf.max_fn_params_bools,
10296
}
10397
}
98+
}
10499

105-
fn too_many_bools<'tcx>(&self, tys: impl Iterator<Item = &'tcx Ty<'tcx>>, kind: Kind) -> bool {
106-
if let Ok(bools) = tys.filter(|ty| is_bool(ty)).count().try_into() {
107-
(if Kind::Fn == kind {
108-
self.max_fn_params_bools
109-
} else {
110-
self.max_struct_bools
111-
}) < bools
112-
} else {
113-
false
114-
}
115-
}
100+
impl_lint_pass!(ExcessiveBools => [STRUCT_EXCESSIVE_BOOLS, FN_PARAMS_EXCESSIVE_BOOLS]);
116101

117-
fn check_fn_sig(&self, cx: &LateContext<'_>, fn_decl: &FnDecl<'_>, span: Span) {
118-
if !span.from_expansion() && self.too_many_bools(fn_decl.inputs.iter(), Kind::Fn) {
119-
span_lint_and_help(
120-
cx,
121-
FN_PARAMS_EXCESSIVE_BOOLS,
122-
span,
123-
format!("more than {} bools in function parameters", self.max_fn_params_bools),
124-
None,
125-
"consider refactoring bools into two-variant enums",
126-
);
127-
}
128-
}
102+
fn has_n_bools<'tcx>(iter: impl Iterator<Item = &'tcx Ty<'tcx>>, mut count: u64) -> bool {
103+
iter.filter(|ty| is_bool(ty)).any(|_| {
104+
let (x, overflow) = count.overflowing_sub(1);
105+
count = x;
106+
overflow
107+
})
129108
}
130109

131-
impl_lint_pass!(ExcessiveBools => [STRUCT_EXCESSIVE_BOOLS, FN_PARAMS_EXCESSIVE_BOOLS]);
110+
fn check_fn_decl(cx: &LateContext<'_>, decl: &FnDecl<'_>, sp: Span, max: u64) {
111+
if has_n_bools(decl.inputs.iter(), max) && !sp.from_expansion() {
112+
span_lint_and_help(
113+
cx,
114+
FN_PARAMS_EXCESSIVE_BOOLS,
115+
sp,
116+
format!("more than {max} bools in function parameters"),
117+
None,
118+
"consider refactoring bools into two-variant enums",
119+
);
120+
}
121+
}
132122

133123
impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
134124
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
135-
if item.span.from_expansion() {
136-
return;
137-
}
138-
if let ItemKind::Struct(variant_data, _) = &item.kind {
139-
if has_repr_attr(cx, item.hir_id()) {
140-
return;
141-
}
142-
143-
if self.too_many_bools(variant_data.fields().iter().map(|field| field.ty), Kind::Struct) {
144-
span_lint_and_help(
145-
cx,
146-
STRUCT_EXCESSIVE_BOOLS,
147-
item.span,
148-
format!("more than {} bools in a struct", self.max_struct_bools),
149-
None,
150-
"consider using a state machine or refactoring bools into two-variant enums",
151-
);
152-
}
125+
if let ItemKind::Struct(variant_data, _) = &item.kind
126+
&& variant_data.fields().len() as u64 > self.max_struct_bools
127+
&& has_n_bools(
128+
variant_data.fields().iter().map(|field| field.ty),
129+
self.max_struct_bools,
130+
)
131+
&& !has_repr_attr(cx, item.hir_id())
132+
&& !item.span.from_expansion()
133+
{
134+
span_lint_and_help(
135+
cx,
136+
STRUCT_EXCESSIVE_BOOLS,
137+
item.span,
138+
format!("more than {} bools in a struct", self.max_struct_bools),
139+
None,
140+
"consider using a state machine or refactoring bools into two-variant enums",
141+
);
153142
}
154143
}
155144

156145
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx TraitItem<'tcx>) {
157146
// functions with a body are already checked by `check_fn`
158147
if let TraitItemKind::Fn(fn_sig, TraitFn::Required(_)) = &trait_item.kind
159148
&& fn_sig.header.abi == Abi::Rust
149+
&& fn_sig.decl.inputs.len() as u64 > self.max_fn_params_bools
160150
{
161-
self.check_fn_sig(cx, fn_sig.decl, fn_sig.span);
151+
check_fn_decl(cx, fn_sig.decl, fn_sig.span, self.max_fn_params_bools);
162152
}
163153
}
164154

@@ -171,12 +161,13 @@ impl<'tcx> LateLintPass<'tcx> for ExcessiveBools {
171161
span: Span,
172162
def_id: LocalDefId,
173163
) {
174-
let hir_id = cx.tcx.local_def_id_to_hir_id(def_id);
175164
if let Some(fn_header) = fn_kind.header()
176165
&& fn_header.abi == Abi::Rust
177-
&& get_parent_as_impl(cx.tcx, hir_id).map_or(true, |impl_item| impl_item.of_trait.is_none())
166+
&& fn_decl.inputs.len() as u64 > self.max_fn_params_bools
167+
&& get_parent_as_impl(cx.tcx, cx.tcx.local_def_id_to_hir_id(def_id))
168+
.map_or(true, |impl_item| impl_item.of_trait.is_none())
178169
{
179-
self.check_fn_sig(cx, fn_decl, span);
170+
check_fn_decl(cx, fn_decl, span, self.max_fn_params_bools);
180171
}
181172
}
182173
}

clippy_lints/src/extra_unused_type_parameters.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,6 @@ impl ExtraUnusedTypeParameters {
5151
avoid_breaking_exported_api: conf.avoid_breaking_exported_api,
5252
}
5353
}
54-
55-
/// Don't lint external macros or functions with empty bodies. Also, don't lint exported items
56-
/// if the `avoid_breaking_exported_api` config option is set.
57-
fn is_empty_exported_or_macro(
58-
&self,
59-
cx: &LateContext<'_>,
60-
span: Span,
61-
def_id: LocalDefId,
62-
body_id: BodyId,
63-
) -> bool {
64-
let body = cx.tcx.hir().body(body_id).value;
65-
let fn_empty = matches!(&body.kind, ExprKind::Block(blk, None) if blk.stmts.is_empty() && blk.expr.is_none());
66-
let is_exported = cx.effective_visibilities.is_exported(def_id);
67-
in_external_macro(cx.sess(), span) || fn_empty || (is_exported && self.avoid_breaking_exported_api)
68-
}
6954
}
7055

7156
impl_lint_pass!(ExtraUnusedTypeParameters => [EXTRA_UNUSED_TYPE_PARAMETERS]);
@@ -267,10 +252,17 @@ impl<'cx, 'tcx> Visitor<'tcx> for TypeWalker<'cx, 'tcx> {
267252
}
268253
}
269254

255+
fn is_empty_body(cx: &LateContext<'_>, body: BodyId) -> bool {
256+
matches!(cx.tcx.hir().body(body).value.kind, ExprKind::Block(b, _) if b.stmts.is_empty() && b.expr.is_none())
257+
}
258+
270259
impl<'tcx> LateLintPass<'tcx> for ExtraUnusedTypeParameters {
271260
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
272261
if let ItemKind::Fn(_, generics, body_id) = item.kind
273-
&& !self.is_empty_exported_or_macro(cx, item.span, item.owner_id.def_id, body_id)
262+
&& !generics.params.is_empty()
263+
&& !is_empty_body(cx, body_id)
264+
&& (!self.avoid_breaking_exported_api || !cx.effective_visibilities.is_exported(item.owner_id.def_id))
265+
&& !in_external_macro(cx.sess(), item.span)
274266
&& !is_from_proc_macro(cx, item)
275267
{
276268
let mut walker = TypeWalker::new(cx, generics);
@@ -282,8 +274,12 @@ impl<'tcx> LateLintPass<'tcx> for ExtraUnusedTypeParameters {
282274
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'tcx>) {
283275
// Only lint on inherent methods, not trait methods.
284276
if let ImplItemKind::Fn(.., body_id) = item.kind
277+
&& !item.generics.params.is_empty()
285278
&& trait_ref_of_method(cx, item.owner_id.def_id).is_none()
286-
&& !self.is_empty_exported_or_macro(cx, item.span, item.owner_id.def_id, body_id)
279+
&& !is_empty_body(cx, body_id)
280+
&& (!self.avoid_breaking_exported_api || !cx.effective_visibilities.is_exported(item.owner_id.def_id))
281+
&& !in_external_macro(cx.sess(), item.span)
282+
&& !is_from_proc_macro(cx, item)
287283
{
288284
let mut walker = TypeWalker::new(cx, item.generics);
289285
walk_impl_item(&mut walker, item);

clippy_lints/src/future_not_send.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,11 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend {
6666
let ret_ty = return_ty(cx, cx.tcx.local_def_id_to_hir_id(fn_def_id).expect_owner());
6767
if let ty::Alias(ty::Opaque, AliasTy { def_id, args, .. }) = *ret_ty.kind() {
6868
let preds = cx.tcx.explicit_item_super_predicates(def_id);
69-
let mut is_future = false;
70-
for (p, _span) in preds.iter_instantiated_copied(cx.tcx, args) {
71-
if let Some(trait_pred) = p.as_trait_clause() {
72-
if Some(trait_pred.skip_binder().trait_ref.def_id) == cx.tcx.lang_items().future_trait() {
73-
is_future = true;
74-
break;
75-
}
76-
}
77-
}
69+
let is_future = preds.iter_instantiated_copied(cx.tcx, args).any(|(p, _)| {
70+
p.as_trait_clause().is_some_and(|trait_pred| {
71+
Some(trait_pred.skip_binder().trait_ref.def_id) == cx.tcx.lang_items().future_trait()
72+
})
73+
});
7874
if is_future {
7975
let send_trait = cx.tcx.get_diagnostic_item(sym::Send).unwrap();
8076
let span = decl.output.span();

0 commit comments

Comments
 (0)