Skip to content

Commit 4914833

Browse files
committed
Auto merge of #9279 - tabokie:weight-loss, r=flip1995
chore: a few small improvements to code quality Some improvements: - Simplify implementation of `is_unit_type` - Use slice matching to destruct `Call` or `MethodCall` whenever possible changelog: none r? `@flip1995`
2 parents 367d09f + ac7a91e commit 4914833

19 files changed

+60
-91
lines changed

clippy_lints/src/checked_conversions.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,7 @@ fn get_types_from_cast<'a>(
270270
let limit_from: Option<(&Expr<'_>, &str)> = call_from_cast.or_else(|| {
271271
if_chain! {
272272
// `from_type::from, to_type::max_value()`
273-
if let ExprKind::Call(from_func, args) = &expr.kind;
274-
// `to_type::max_value()`
275-
if args.len() == 1;
276-
if let limit = &args[0];
273+
if let ExprKind::Call(from_func, [limit]) = &expr.kind;
277274
// `from_type::from`
278275
if let ExprKind::Path(ref path) = &from_func.kind;
279276
if let Some(from_sym) = get_implementing_type(path, INTS, "from");

clippy_lints/src/create_dir.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ declare_lint_pass!(CreateDir => [CREATE_DIR]);
3434
impl LateLintPass<'_> for CreateDir {
3535
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
3636
if_chain! {
37-
if let ExprKind::Call(func, args) = expr.kind;
37+
if let ExprKind::Call(func, [arg, ..]) = expr.kind;
3838
if let ExprKind::Path(ref path) = func.kind;
3939
if let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id();
4040
if match_def_path(cx, def_id, &paths::STD_FS_CREATE_DIR);
@@ -45,7 +45,7 @@ impl LateLintPass<'_> for CreateDir {
4545
expr.span,
4646
"calling `std::fs::create_dir` where there may be a better way",
4747
"consider calling `std::fs::create_dir_all` instead",
48-
format!("create_dir_all({})", snippet(cx, args[0].span, "..")),
48+
format!("create_dir_all({})", snippet(cx, arg.span, "..")),
4949
Applicability::MaybeIncorrect,
5050
)
5151
}

clippy_lints/src/from_str_radix_10.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ declare_lint_pass!(FromStrRadix10 => [FROM_STR_RADIX_10]);
4646
impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
4747
fn check_expr(&mut self, cx: &LateContext<'tcx>, exp: &Expr<'tcx>) {
4848
if_chain! {
49-
if let ExprKind::Call(maybe_path, arguments) = &exp.kind;
49+
if let ExprKind::Call(maybe_path, [src, radix]) = &exp.kind;
5050
if let ExprKind::Path(QPath::TypeRelative(ty, pathseg)) = &maybe_path.kind;
5151

5252
// check if the first part of the path is some integer primitive
@@ -60,20 +60,19 @@ impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
6060
if pathseg.ident.name.as_str() == "from_str_radix";
6161

6262
// check if the second argument is a primitive `10`
63-
if arguments.len() == 2;
64-
if let ExprKind::Lit(lit) = &arguments[1].kind;
63+
if let ExprKind::Lit(lit) = &radix.kind;
6564
if let rustc_ast::ast::LitKind::Int(10, _) = lit.node;
6665

6766
then {
68-
let expr = if let ExprKind::AddrOf(_, _, expr) = &arguments[0].kind {
67+
let expr = if let ExprKind::AddrOf(_, _, expr) = &src.kind {
6968
let ty = cx.typeck_results().expr_ty(expr);
7069
if is_ty_stringish(cx, ty) {
7170
expr
7271
} else {
73-
&arguments[0]
72+
&src
7473
}
7574
} else {
76-
&arguments[0]
75+
&src
7776
};
7877

7978
let sugg = Sugg::hir_with_applicability(

clippy_lints/src/loops/manual_memcpy.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,9 @@ fn build_manual_memcpy_suggestion<'tcx>(
119119

120120
let print_limit = |end: &Expr<'_>, end_str: &str, base: &Expr<'_>, sugg: MinifyingSugg<'static>| {
121121
if_chain! {
122-
if let ExprKind::MethodCall(method, len_args, _) = end.kind;
122+
if let ExprKind::MethodCall(method, [recv], _) = end.kind;
123123
if method.ident.name == sym::len;
124-
if len_args.len() == 1;
125-
if let Some(arg) = len_args.get(0);
126-
if path_to_local(arg) == path_to_local(base);
124+
if path_to_local(recv) == path_to_local(base);
127125
then {
128126
if sugg.to_string() == end_str {
129127
sugg::EMPTY.into()
@@ -343,10 +341,8 @@ fn get_slice_like_element_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Opti
343341

344342
fn fetch_cloned_expr<'tcx>(expr: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
345343
if_chain! {
346-
if let ExprKind::MethodCall(method, args, _) = expr.kind;
344+
if let ExprKind::MethodCall(method, [arg], _) = expr.kind;
347345
if method.ident.name == sym::clone;
348-
if args.len() == 1;
349-
if let Some(arg) = args.get(0);
350346
then { arg } else { expr }
351347
}
352348
}

clippy_lints/src/loops/needless_range_loop.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,9 @@ pub(super) fn check<'tcx>(
188188

189189
fn is_len_call(expr: &Expr<'_>, var: Symbol) -> bool {
190190
if_chain! {
191-
if let ExprKind::MethodCall(method, len_args, _) = expr.kind;
192-
if len_args.len() == 1;
191+
if let ExprKind::MethodCall(method, [recv], _) = expr.kind;
193192
if method.ident.name == sym::len;
194-
if let ExprKind::Path(QPath::Resolved(_, path)) = len_args[0].kind;
193+
if let ExprKind::Path(QPath::Resolved(_, path)) = recv.kind;
195194
if path.segments.len() == 1;
196195
if path.segments[0].ident.name == var;
197196
then {

clippy_lints/src/manual_ok_or.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,14 @@ impl<'tcx> LateLintPass<'tcx> for ManualOkOr {
4747
}
4848

4949
if_chain! {
50-
if let ExprKind::MethodCall(method_segment, args, _) = scrutinee.kind;
50+
if let ExprKind::MethodCall(method_segment, [receiver, or_expr, map_expr], _) = scrutinee.kind;
5151
if method_segment.ident.name == sym!(map_or);
52-
if args.len() == 3;
53-
let method_receiver = &args[0];
54-
let ty = cx.typeck_results().expr_ty(method_receiver);
52+
let ty = cx.typeck_results().expr_ty(receiver);
5553
if is_type_diagnostic_item(cx, ty, sym::Option);
56-
let or_expr = &args[1];
57-
if is_ok_wrapping(cx, &args[2]);
54+
if is_ok_wrapping(cx, map_expr);
5855
if let ExprKind::Call(Expr { kind: ExprKind::Path(err_path), .. }, &[ref err_arg]) = or_expr.kind;
5956
if is_lang_ctor(cx, err_path, ResultErr);
60-
if let Some(method_receiver_snippet) = snippet_opt(cx, method_receiver.span);
57+
if let Some(method_receiver_snippet) = snippet_opt(cx, receiver.span);
6158
if let Some(err_arg_snippet) = snippet_opt(cx, err_arg.span);
6259
if let Some(indent) = indent_of(cx, scrutinee.span);
6360
then {

clippy_lints/src/map_err_ignore.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,18 @@ impl<'tcx> LateLintPass<'tcx> for MapErrIgnore {
113113
}
114114

115115
// check if this is a method call (e.g. x.foo())
116-
if let ExprKind::MethodCall(method, args, _) = e.kind {
116+
if let ExprKind::MethodCall(method, [_, arg], _) = e.kind {
117117
// only work if the method name is `map_err` and there are only 2 arguments (e.g. x.map_err(|_|[1]
118118
// Enum::Variant[2]))
119-
if method.ident.as_str() == "map_err" && args.len() == 2 {
119+
if method.ident.name == sym!(map_err) {
120120
// make sure the first argument is a closure, and grab the CaptureRef, BodyId, and fn_decl_span
121121
// fields
122122
if let ExprKind::Closure(&Closure {
123123
capture_clause,
124124
body,
125125
fn_decl_span,
126126
..
127-
}) = args[1].kind
127+
}) = arg.kind
128128
{
129129
// check if this is by Reference (meaning there's no move statement)
130130
if capture_clause == CaptureBy::Ref {

clippy_lints/src/map_unit_fn.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ declare_clippy_lint! {
9797
declare_lint_pass!(MapUnit => [OPTION_MAP_UNIT_FN, RESULT_MAP_UNIT_FN]);
9898

9999
fn is_unit_type(ty: Ty<'_>) -> bool {
100-
match ty.kind() {
101-
ty::Tuple(slice) => slice.is_empty(),
102-
ty::Never => true,
103-
_ => false,
104-
}
100+
ty.is_unit() || ty.is_never()
105101
}
106102

107103
fn is_unit_function(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> bool {

clippy_lints/src/matches/match_as_ref.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ fn is_ref_some_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> Option<BindingAnnotat
7272
if is_lang_ctor(cx, qpath, LangItem::OptionSome);
7373
if let PatKind::Binding(rb, .., ident, _) = first_pat.kind;
7474
if rb == BindingAnnotation::Ref || rb == BindingAnnotation::RefMut;
75-
if let ExprKind::Call(e, args) = peel_blocks(arm.body).kind;
75+
if let ExprKind::Call(e, [arg]) = peel_blocks(arm.body).kind;
7676
if let ExprKind::Path(ref some_path) = e.kind;
77-
if is_lang_ctor(cx, some_path, LangItem::OptionSome) && args.len() == 1;
78-
if let ExprKind::Path(QPath::Resolved(_, path2)) = args[0].kind;
77+
if is_lang_ctor(cx, some_path, LangItem::OptionSome);
78+
if let ExprKind::Path(QPath::Resolved(_, path2)) = arg.kind;
7979
if path2.segments.len() == 1 && ident.name == path2.segments[0].ident.name;
8080
then {
8181
return Some(rb)

clippy_lints/src/matches/try_err.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
2323
// val,
2424
// };
2525
if_chain! {
26-
if let ExprKind::Call(match_fun, try_args) = scrutinee.kind;
26+
if let ExprKind::Call(match_fun, [try_arg, ..]) = scrutinee.kind;
2727
if let ExprKind::Path(ref match_fun_path) = match_fun.kind;
2828
if matches!(match_fun_path, QPath::LangItem(LangItem::TryTraitBranch, ..));
29-
if let Some(try_arg) = try_args.get(0);
30-
if let ExprKind::Call(err_fun, err_args) = try_arg.kind;
31-
if let Some(err_arg) = err_args.get(0);
29+
if let ExprKind::Call(err_fun, [err_arg, ..]) = try_arg.kind;
3230
if let ExprKind::Path(ref err_fun_path) = err_fun.kind;
3331
if is_lang_ctor(cx, err_fun_path, ResultErr);
3432
if let Some(return_ty) = find_return_type(cx, &expr.kind);

0 commit comments

Comments
 (0)