Skip to content

Commit 8f9c738

Browse files
committed
dogfood clippy
1 parent 1b4e2ef commit 8f9c738

File tree

2 files changed

+57
-48
lines changed

2 files changed

+57
-48
lines changed

clippy_lints/src/manual_async_fn.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,11 @@ fn desugared_async_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>)
187187
}
188188

189189
fn suggested_ret(cx: &LateContext<'_>, output: &Ty<'_>) -> Option<(&'static str, String)> {
190-
match output.kind {
191-
TyKind::Tup(tys) if tys.is_empty() => {
192-
let sugg = "remove the return type";
193-
Some((sugg, String::new()))
194-
},
195-
_ => {
196-
let sugg = "return the output of the future directly";
197-
snippet_opt(cx, output.span).map(|snip| (sugg, format!(" -> {snip}")))
198-
},
190+
if let TyKind::Tup([]) = output.kind {
191+
let sugg = "remove the return type";
192+
Some((sugg, String::new()))
193+
} else {
194+
let sugg = "return the output of the future directly";
195+
snippet_opt(cx, output.span).map(|snip| (sugg, format!(" -> {snip}")))
199196
}
200197
}

clippy_lints/src/matches/redundant_guards.rs

+51-39
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::def::{DefKind, Res};
88
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, Guard, MatchSource, Node, Pat, PatKind};
99
use rustc_lint::LateContext;
1010
use rustc_span::symbol::Ident;
11-
use rustc_span::Span;
11+
use rustc_span::{Span, Symbol};
1212
use std::borrow::Cow;
1313
use std::ops::ControlFlow;
1414

@@ -105,50 +105,62 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
105105
&& let ExprKind::MethodCall(path, recv, args, ..) = if_expr.kind
106106
&& let Some(binding) = get_pat_binding(cx, recv, outer_arm)
107107
{
108-
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
109-
let slice_like = ty.is_slice() || ty.is_array();
110-
111-
let sugg = if path.ident.name == sym!(is_empty) {
112-
// `s if s.is_empty()` becomes ""
113-
// `arr if arr.is_empty()` becomes []
108+
check_method_calls(cx, outer_arm, path.ident.name, recv, args, if_expr, &binding);
109+
}
110+
}
111+
}
114112

115-
if ty.is_str() {
116-
r#""""#.into()
117-
} else if slice_like {
118-
"[]".into()
119-
} else {
120-
continue;
121-
}
122-
} else if slice_like
123-
&& let Some(needle) = args.first()
124-
&& let ExprKind::AddrOf(.., needle) = needle.kind
125-
&& let ExprKind::Array(needles) = needle.kind
126-
&& needles.iter().all(|needle| expr_can_be_pat(cx, needle))
127-
{
128-
// `arr if arr.starts_with(&[123])` becomes [123, ..]
129-
// `arr if arr.ends_with(&[123])` becomes [.., 123]
130-
// `arr if arr.starts_with(&[])` becomes [..] (why would anyone write this?)
113+
fn check_method_calls<'tcx>(
114+
cx: &LateContext<'tcx>,
115+
arm: &Arm<'tcx>,
116+
method: Symbol,
117+
recv: &Expr<'_>,
118+
args: &[Expr<'_>],
119+
if_expr: &Expr<'_>,
120+
binding: &PatBindingInfo,
121+
) {
122+
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
123+
let slice_like = ty.is_slice() || ty.is_array();
131124

132-
let mut sugg = snippet(cx, needle.span, "<needle>").into_owned();
125+
let sugg = if method == sym!(is_empty) {
126+
// `s if s.is_empty()` becomes ""
127+
// `arr if arr.is_empty()` becomes []
133128

134-
if needles.is_empty() {
135-
sugg.insert_str(1, "..");
136-
} else if path.ident.name == sym!(starts_with) {
137-
sugg.insert_str(sugg.len() - 1, ", ..");
138-
} else if path.ident.name == sym!(ends_with) {
139-
sugg.insert_str(1, ".., ");
140-
} else {
141-
continue;
142-
}
129+
if ty.is_str() {
130+
r#""""#.into()
131+
} else if slice_like {
132+
"[]".into()
133+
} else {
134+
return;
135+
}
136+
} else if slice_like
137+
&& let Some(needle) = args.first()
138+
&& let ExprKind::AddrOf(.., needle) = needle.kind
139+
&& let ExprKind::Array(needles) = needle.kind
140+
&& needles.iter().all(|needle| expr_can_be_pat(cx, needle))
141+
{
142+
// `arr if arr.starts_with(&[123])` becomes [123, ..]
143+
// `arr if arr.ends_with(&[123])` becomes [.., 123]
144+
// `arr if arr.starts_with(&[])` becomes [..] (why would anyone write this?)
143145

144-
sugg.into()
145-
} else {
146-
continue;
147-
};
146+
let mut sugg = snippet(cx, needle.span, "<needle>").into_owned();
148147

149-
emit_redundant_guards(cx, outer_arm, if_expr.span, sugg, &binding, None);
148+
if needles.is_empty() {
149+
sugg.insert_str(1, "..");
150+
} else if method == sym!(starts_with) {
151+
sugg.insert_str(sugg.len() - 1, ", ..");
152+
} else if method == sym!(ends_with) {
153+
sugg.insert_str(1, ".., ");
154+
} else {
155+
return;
150156
}
151-
}
157+
158+
sugg.into()
159+
} else {
160+
return;
161+
};
162+
163+
emit_redundant_guards(cx, arm, if_expr.span, sugg, binding, None);
152164
}
153165

154166
struct PatBindingInfo {

0 commit comments

Comments
 (0)