Skip to content

Commit

Permalink
Also ignore field initialization as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jul 16, 2023
1 parent 8ad609f commit ce15b65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
14 changes: 10 additions & 4 deletions clippy_lints/src/single_range_in_vec_init.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::get_trait_def_id;
use clippy_utils::higher::VecArgs;
use clippy_utils::macros::root_macro_call_first_node;
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::implements_trait;
use clippy_utils::{get_parent_node, get_trait_def_id, peel_blocks};
use rustc_ast::{LitIntType, LitKind, UintTy};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, LangItem, Local, Node, QPath};
Expand Down Expand Up @@ -183,11 +183,17 @@ fn has_type_annotations(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
peel_blocks(init).hir_id == expr.hir_id && local.ty.is_some()
}

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

matches!(parent.kind, ExprKind::Call(_, _) | ExprKind::MethodCall(..))
matches!(
parent,
Node::Expr(e) if matches!(e.kind, ExprKind::Call(_, _) | ExprKind::MethodCall(..)))
// NOTE: This will ignore anything akin to `Vec<&dyn Any>` as well, which could ideally be
// linted as well, but this should be fine due to the rarity of those circumstances
|| matches!(parent, Node::ExprField(_))
}
12 changes: 12 additions & 0 deletions tests/ui/single_range_in_vec_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ fn main() {
vec![0.0..200.0];
// Issue #11086
let do_not_lint_if_has_type_annotations: Vec<Range<_>> = vec![0..200];
// https://github.com/rust-lang/rust-clippy/issues/11086#issuecomment-1636996525
struct DoNotLintStructInitializersUnit(Vec<Range<usize>>);
struct DoNotLintStructInitializersNamed {
a: Vec<Range<usize>>,
};
enum DoNotLintEnums {
One(Vec<Range<usize>>),
Two(Vec<Range<usize>>),
}
DoNotLintStructInitializersUnit(vec![0..200]);
DoNotLintStructInitializersNamed { a: vec![0..200] };
DoNotLintEnums::One(vec![0..200]);
do_not_lint_as_argument(vec![0..200]);
// `Copy` is not implemented for `Range`, so this doesn't matter
// FIXME: [0..200; 2];
Expand Down

0 comments on commit ce15b65

Please sign in to comment.