dogfood clippy

This commit is contained in:
y21 2023-11-15 21:34:48 +01:00
parent 1b4e2ef3d7
commit 8f9c738ce9
2 changed files with 61 additions and 52 deletions

View file

@ -187,14 +187,11 @@ fn desugared_async_block<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>)
}
fn suggested_ret(cx: &LateContext<'_>, output: &Ty<'_>) -> Option<(&'static str, String)> {
match output.kind {
TyKind::Tup(tys) if tys.is_empty() => {
let sugg = "remove the return type";
Some((sugg, String::new()))
},
_ => {
let sugg = "return the output of the future directly";
snippet_opt(cx, output.span).map(|snip| (sugg, format!(" -> {snip}")))
},
if let TyKind::Tup([]) = output.kind {
let sugg = "remove the return type";
Some((sugg, String::new()))
} else {
let sugg = "return the output of the future directly";
snippet_opt(cx, output.span).map(|snip| (sugg, format!(" -> {snip}")))
}
}

View file

@ -8,7 +8,7 @@ use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Arm, BinOpKind, Expr, ExprKind, Guard, MatchSource, Node, Pat, PatKind};
use rustc_lint::LateContext;
use rustc_span::symbol::Ident;
use rustc_span::Span;
use rustc_span::{Span, Symbol};
use std::borrow::Cow;
use std::ops::ControlFlow;
@ -105,52 +105,64 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'tcx>]) {
&& let ExprKind::MethodCall(path, recv, args, ..) = if_expr.kind
&& let Some(binding) = get_pat_binding(cx, recv, outer_arm)
{
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
let slice_like = ty.is_slice() || ty.is_array();
let sugg = if path.ident.name == sym!(is_empty) {
// `s if s.is_empty()` becomes ""
// `arr if arr.is_empty()` becomes []
if ty.is_str() {
r#""""#.into()
} else if slice_like {
"[]".into()
} else {
continue;
}
} else if slice_like
&& let Some(needle) = args.first()
&& let ExprKind::AddrOf(.., needle) = needle.kind
&& let ExprKind::Array(needles) = needle.kind
&& needles.iter().all(|needle| expr_can_be_pat(cx, needle))
{
// `arr if arr.starts_with(&[123])` becomes [123, ..]
// `arr if arr.ends_with(&[123])` becomes [.., 123]
// `arr if arr.starts_with(&[])` becomes [..] (why would anyone write this?)
let mut sugg = snippet(cx, needle.span, "<needle>").into_owned();
if needles.is_empty() {
sugg.insert_str(1, "..");
} else if path.ident.name == sym!(starts_with) {
sugg.insert_str(sugg.len() - 1, ", ..");
} else if path.ident.name == sym!(ends_with) {
sugg.insert_str(1, ".., ");
} else {
continue;
}
sugg.into()
} else {
continue;
};
emit_redundant_guards(cx, outer_arm, if_expr.span, sugg, &binding, None);
check_method_calls(cx, outer_arm, path.ident.name, recv, args, if_expr, &binding);
}
}
}
fn check_method_calls<'tcx>(
cx: &LateContext<'tcx>,
arm: &Arm<'tcx>,
method: Symbol,
recv: &Expr<'_>,
args: &[Expr<'_>],
if_expr: &Expr<'_>,
binding: &PatBindingInfo,
) {
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
let slice_like = ty.is_slice() || ty.is_array();
let sugg = if method == sym!(is_empty) {
// `s if s.is_empty()` becomes ""
// `arr if arr.is_empty()` becomes []
if ty.is_str() {
r#""""#.into()
} else if slice_like {
"[]".into()
} else {
return;
}
} else if slice_like
&& let Some(needle) = args.first()
&& let ExprKind::AddrOf(.., needle) = needle.kind
&& let ExprKind::Array(needles) = needle.kind
&& needles.iter().all(|needle| expr_can_be_pat(cx, needle))
{
// `arr if arr.starts_with(&[123])` becomes [123, ..]
// `arr if arr.ends_with(&[123])` becomes [.., 123]
// `arr if arr.starts_with(&[])` becomes [..] (why would anyone write this?)
let mut sugg = snippet(cx, needle.span, "<needle>").into_owned();
if needles.is_empty() {
sugg.insert_str(1, "..");
} else if method == sym!(starts_with) {
sugg.insert_str(sugg.len() - 1, ", ..");
} else if method == sym!(ends_with) {
sugg.insert_str(1, ".., ");
} else {
return;
}
sugg.into()
} else {
return;
};
emit_redundant_guards(cx, arm, if_expr.span, sugg, binding, None);
}
struct PatBindingInfo {
span: Span,
byref_ident: Option<Ident>,