Skip to content

Commit 336fa85

Browse files
committed
Also ignore field initialization as well
1 parent 8ad609f commit 336fa85

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

clippy_lints/src/single_range_in_vec_init.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::get_trait_def_id;
32
use clippy_utils::higher::VecArgs;
43
use clippy_utils::macros::root_macro_call_first_node;
54
use clippy_utils::source::snippet_opt;
65
use clippy_utils::ty::implements_trait;
6+
use clippy_utils::{get_parent_node, get_trait_def_id, peel_blocks};
77
use rustc_ast::{LitIntType, LitKind, UintTy};
88
use rustc_errors::Applicability;
99
use rustc_hir::{Expr, ExprKind, LangItem, Local, Node, QPath};
@@ -183,11 +183,17 @@ fn has_type_annotations(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
183183
peel_blocks(init).hir_id == expr.hir_id && local.ty.is_some()
184184
}
185185

186-
/// Returns whether `expr` is used as an argument to a function. We should not lint this.
186+
/// Returns whether `expr` is used as an argument to a function or initializing a struct/enum in any
187+
/// way. We should not lint this.
187188
fn is_argument(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
188-
let Some(parent) = get_parent_expr(cx, expr) else {
189+
let Some(parent) = get_parent_node(cx.tcx, expr.hir_id) else {
189190
return false;
190191
};
191192

192-
matches!(parent.kind, ExprKind::Call(_, _) | ExprKind::MethodCall(..))
193+
matches!(
194+
parent,
195+
Node::Expr(e) if matches!(e.kind, ExprKind::Call(_, _) | ExprKind::MethodCall(..) | ExprKind::Assign(_, _, _)))
196+
// NOTE: This will ignore anything akin to `Vec<&dyn Any>` as well, which could ideally be
197+
// linted as well, but this should be fine due to the rarity of those circumstances
198+
|| matches!(parent, Node::ExprField(_))
193199
}

tests/ui/single_range_in_vec_init.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@aux-build:proc_macros.rs:proc-macro
2-
#![allow(clippy::no_effect, clippy::useless_vec, unused)]
2+
#![allow(clippy::temporary_assignment, clippy::no_effect, clippy::useless_vec, unused)]
33
#![warn(clippy::single_range_in_vec_init)]
44
#![feature(generic_arg_infer)]
55

@@ -47,6 +47,22 @@ fn main() {
4747
vec![0.0..200.0];
4848
// Issue #11086
4949
let do_not_lint_if_has_type_annotations: Vec<Range<_>> = vec![0..200];
50+
// https://github.com/rust-lang/rust-clippy/issues/11086#issuecomment-1636996525
51+
struct DoNotLintStructInitializersUnit(Vec<Range<usize>>);
52+
struct DoNotLintStructInitializersNamed {
53+
a: Vec<Range<usize>>,
54+
};
55+
enum DoNotLintEnums {
56+
One(Vec<Range<usize>>),
57+
Two(Vec<Range<usize>>),
58+
}
59+
DoNotLintStructInitializersUnit(vec![0..200]).0 = vec![0..200];
60+
DoNotLintStructInitializersNamed { a: vec![0..200] }.a = vec![0..200];
61+
let o = DoNotLintEnums::One(vec![0..200]);
62+
match o {
63+
DoNotLintEnums::One(mut e) => e = vec![0..200],
64+
_ => todo!(),
65+
}
5066
do_not_lint_as_argument(vec![0..200]);
5167
// `Copy` is not implemented for `Range`, so this doesn't matter
5268
// FIXME: [0..200; 2];

0 commit comments

Comments
 (0)