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);

clippy_lints/src/mem_replace.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ fn check_replace_with_uninit(cx: &LateContext<'_>, src: &Expr<'_>, dest: &Expr<'
163163
}
164164

165165
if_chain! {
166-
if let ExprKind::Call(repl_func, repl_args) = src.kind;
167-
if repl_args.is_empty();
166+
if let ExprKind::Call(repl_func, []) = src.kind;
168167
if let ExprKind::Path(ref repl_func_qpath) = repl_func.kind;
169168
if let Some(repl_def_id) = cx.qpath_res(repl_func_qpath, repl_func.hir_id).opt_def_id();
170169
then {
@@ -246,11 +245,10 @@ impl<'tcx> LateLintPass<'tcx> for MemReplace {
246245
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
247246
if_chain! {
248247
// Check that `expr` is a call to `mem::replace()`
249-
if let ExprKind::Call(func, func_args) = expr.kind;
248+
if let ExprKind::Call(func, [dest, src]) = expr.kind;
250249
if let ExprKind::Path(ref func_qpath) = func.kind;
251250
if let Some(def_id) = cx.qpath_res(func_qpath, func.hir_id).opt_def_id();
252251
if cx.tcx.is_diagnostic_item(sym::mem_replace, def_id);
253-
if let [dest, src] = func_args;
254252
then {
255253
check_replace_option_with_none(cx, src, dest, expr.span);
256254
check_replace_with_uninit(cx, src, dest, expr.span);

clippy_lints/src/path_buf_push_overwrite.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,9 @@ declare_lint_pass!(PathBufPushOverwrite => [PATH_BUF_PUSH_OVERWRITE]);
4646
impl<'tcx> LateLintPass<'tcx> for PathBufPushOverwrite {
4747
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
4848
if_chain! {
49-
if let ExprKind::MethodCall(path, args, _) = expr.kind;
49+
if let ExprKind::MethodCall(path, [recv, get_index_arg], _) = expr.kind;
5050
if path.ident.name == sym!(push);
51-
if args.len() == 2;
52-
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&args[0]).peel_refs(), sym::PathBuf);
53-
if let Some(get_index_arg) = args.get(1);
51+
if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv).peel_refs(), sym::PathBuf);
5452
if let ExprKind::Lit(ref lit) = get_index_arg.kind;
5553
if let LitKind::Str(ref path_lit, _) = lit.node;
5654
if let pushed_path = Path::new(path_lit.as_str());

clippy_lints/src/question_mark.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ fn check_is_none_or_err_and_early_return<'tcx>(cx: &LateContext<'tcx>, expr: &Ex
8686
if_chain! {
8787
if let Some(higher::If { cond, then, r#else }) = higher::If::hir(expr);
8888
if !is_else_clause(cx.tcx, expr);
89-
if let ExprKind::MethodCall(segment, args, _) = &cond.kind;
90-
if let Some(caller) = args.get(0);
89+
if let ExprKind::MethodCall(segment, [caller, ..], _) = &cond.kind;
9190
let caller_ty = cx.typeck_results().expr_ty(caller);
9291
let if_block = IfBlockType::IfIs(caller, caller_ty, segment.ident.name, then, r#else);
9392
if is_early_return(sym::Option, cx, &if_block) || is_early_return(sym::Result, cx, &if_block);

clippy_lints/src/ranges.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,24 +385,24 @@ fn check_range_zip_with_len(cx: &LateContext<'_>, path: &PathSegment<'_>, args:
385385
if path.ident.as_str() == "zip";
386386
if let [iter, zip_arg] = args;
387387
// `.iter()` call
388-
if let ExprKind::MethodCall(iter_path, iter_args, _) = iter.kind;
388+
if let ExprKind::MethodCall(iter_path, [iter_caller, ..], _) = iter.kind;
389389
if iter_path.ident.name == sym::iter;
390390
// range expression in `.zip()` call: `0..x.len()`
391391
if let Some(higher::Range { start: Some(start), end: Some(end), .. }) = higher::Range::hir(zip_arg);
392392
if is_integer_const(cx, start, 0);
393393
// `.len()` call
394-
if let ExprKind::MethodCall(len_path, len_args, _) = end.kind;
395-
if len_path.ident.name == sym::len && len_args.len() == 1;
394+
if let ExprKind::MethodCall(len_path, [len_caller], _) = end.kind;
395+
if len_path.ident.name == sym::len;
396396
// `.iter()` and `.len()` called on same `Path`
397-
if let ExprKind::Path(QPath::Resolved(_, iter_path)) = iter_args[0].kind;
398-
if let ExprKind::Path(QPath::Resolved(_, len_path)) = len_args[0].kind;
397+
if let ExprKind::Path(QPath::Resolved(_, iter_path)) = iter_caller.kind;
398+
if let ExprKind::Path(QPath::Resolved(_, len_path)) = len_caller.kind;
399399
if SpanlessEq::new(cx).eq_path_segments(iter_path.segments, len_path.segments);
400400
then {
401401
span_lint(cx,
402402
RANGE_ZIP_WITH_LEN,
403403
span,
404404
&format!("it is more idiomatic to use `{}.iter().enumerate()`",
405-
snippet(cx, iter_args[0].span, "_"))
405+
snippet(cx, iter_caller.span, "_"))
406406
);
407407
}
408408
}

clippy_lints/src/regex.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,20 @@ declare_lint_pass!(Regex => [INVALID_REGEX, TRIVIAL_REGEX]);
5757
impl<'tcx> LateLintPass<'tcx> for Regex {
5858
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
5959
if_chain! {
60-
if let ExprKind::Call(fun, args) = expr.kind;
60+
if let ExprKind::Call(fun, [arg]) = expr.kind;
6161
if let ExprKind::Path(ref qpath) = fun.kind;
62-
if args.len() == 1;
6362
if let Some(def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id();
6463
then {
6564
if match_def_path(cx, def_id, &paths::REGEX_NEW) ||
6665
match_def_path(cx, def_id, &paths::REGEX_BUILDER_NEW) {
67-
check_regex(cx, &args[0], true);
66+
check_regex(cx, arg, true);
6867
} else if match_def_path(cx, def_id, &paths::REGEX_BYTES_NEW) ||
6968
match_def_path(cx, def_id, &paths::REGEX_BYTES_BUILDER_NEW) {
70-
check_regex(cx, &args[0], false);
69+
check_regex(cx, arg, false);
7170
} else if match_def_path(cx, def_id, &paths::REGEX_SET_NEW) {
72-
check_set(cx, &args[0], true);
71+
check_set(cx, arg, true);
7372
} else if match_def_path(cx, def_id, &paths::REGEX_BYTES_SET_NEW) {
74-
check_set(cx, &args[0], false);
73+
check_set(cx, arg, false);
7574
}
7675
}
7776
}

clippy_lints/src/slow_vector_initialization.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,10 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
233233
/// Returns `true` if give expression is `repeat(0).take(...)`
234234
fn is_repeat_take(&self, expr: &Expr<'_>) -> bool {
235235
if_chain! {
236-
if let ExprKind::MethodCall(take_path, take_args, _) = expr.kind;
236+
if let ExprKind::MethodCall(take_path, [recv, len_arg, ..], _) = expr.kind;
237237
if take_path.ident.name == sym!(take);
238-
239238
// Check that take is applied to `repeat(0)`
240-
if let Some(repeat_expr) = take_args.get(0);
241-
if self.is_repeat_zero(repeat_expr);
242-
243-
if let Some(len_arg) = take_args.get(1);
244-
239+
if self.is_repeat_zero(recv);
245240
then {
246241
// Check that len expression is equals to `with_capacity` expression
247242
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr) {

clippy_lints/src/stable_sort_primitive.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,11 @@ struct LintDetection {
9797

9898
fn detect_stable_sort_primitive(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<LintDetection> {
9999
if_chain! {
100-
if let ExprKind::MethodCall(method_name, args, _) = &expr.kind;
101-
if let Some(slice) = &args.get(0);
100+
if let ExprKind::MethodCall(method_name, [slice, args @ ..], _) = &expr.kind;
102101
if let Some(method) = SortingKind::from_stable_name(method_name.ident.name.as_str());
103102
if let Some(slice_type) = is_slice_of_primitives(cx, slice);
104103
then {
105-
let args_str = args.iter().skip(1).map(|arg| Sugg::hir(cx, arg, "..").to_string()).collect::<Vec<String>>().join(", ");
104+
let args_str = args.iter().map(|arg| Sugg::hir(cx, arg, "..").to_string()).collect::<Vec<String>>().join(", ");
106105
Some(LintDetection { slice_name: Sugg::hir(cx, slice, "..").to_string(), method, method_args: args_str, slice_type })
107106
} else {
108107
None

0 commit comments

Comments
 (0)