Skip to content

Commit ce15b65

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

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
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(..)))
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ 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]);
60+
DoNotLintStructInitializersNamed { a: vec![0..200] };
61+
DoNotLintEnums::One(vec![0..200]);
5062
do_not_lint_as_argument(vec![0..200]);
5163
// `Copy` is not implemented for `Range`, so this doesn't matter
5264
// FIXME: [0..200; 2];

0 commit comments

Comments
 (0)