fix the actual bug
This commit is contained in:
parent
0d83a3a18b
commit
929f746b5c
@ -4,7 +4,7 @@
|
|||||||
use clippy_config::msrvs::{self, Msrv};
|
use clippy_config::msrvs::{self, Msrv};
|
||||||
use clippy_utils::consts::{constant, Constant};
|
use clippy_utils::consts::{constant, Constant};
|
||||||
use clippy_utils::diagnostics::span_lint_hir_and_then;
|
use clippy_utils::diagnostics::span_lint_hir_and_then;
|
||||||
use clippy_utils::source::snippet_with_applicability;
|
use clippy_utils::source::snippet_opt;
|
||||||
use clippy_utils::ty::is_copy;
|
use clippy_utils::ty::is_copy;
|
||||||
use clippy_utils::visitors::for_each_local_use_after_expr;
|
use clippy_utils::visitors::for_each_local_use_after_expr;
|
||||||
use clippy_utils::{get_parent_expr, higher, is_trait_method};
|
use clippy_utils::{get_parent_expr, higher, is_trait_method};
|
||||||
@ -57,6 +57,8 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||||||
let Some(vec_args) = higher::VecArgs::hir(cx, expr.peel_borrows()) else {
|
let Some(vec_args) = higher::VecArgs::hir(cx, expr.peel_borrows()) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
// the parent callsite of this `vec!` expression, or span to the borrowed one such as `&vec!`
|
||||||
|
let callsite = expr.span.parent_callsite().unwrap_or(expr.span);
|
||||||
|
|
||||||
match cx.tcx.parent_hir_node(expr.hir_id) {
|
match cx.tcx.parent_hir_node(expr.hir_id) {
|
||||||
// search for `let foo = vec![_]` expressions where all uses of `foo`
|
// search for `let foo = vec![_]` expressions where all uses of `foo`
|
||||||
@ -84,41 +86,31 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||||||
})
|
})
|
||||||
.is_continue();
|
.is_continue();
|
||||||
|
|
||||||
let span = expr.span.ctxt().outer_expn_data().call_site;
|
|
||||||
if only_slice_uses {
|
if only_slice_uses {
|
||||||
self.check_vec_macro(cx, &vec_args, span, expr.hir_id, SuggestedType::Array);
|
self.check_vec_macro(cx, &vec_args, callsite, expr.hir_id, SuggestedType::Array);
|
||||||
} else {
|
} else {
|
||||||
self.span_to_lint_map.insert(span, None);
|
self.span_to_lint_map.insert(callsite, None);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// if the local pattern has a specified type, do not lint.
|
// if the local pattern has a specified type, do not lint.
|
||||||
Node::Local(Local { ty: Some(_), .. }) if higher::VecArgs::hir(cx, expr).is_some() => {
|
Node::Local(Local { ty: Some(_), .. }) if higher::VecArgs::hir(cx, expr).is_some() => {
|
||||||
let span = expr.span.ctxt().outer_expn_data().call_site;
|
self.span_to_lint_map.insert(callsite, None);
|
||||||
self.span_to_lint_map.insert(span, None);
|
|
||||||
},
|
},
|
||||||
// search for `for _ in vec![...]`
|
// search for `for _ in vec![...]`
|
||||||
Node::Expr(Expr { span, .. })
|
Node::Expr(Expr { span, .. })
|
||||||
if span.is_desugaring(DesugaringKind::ForLoop) && self.msrv.meets(msrvs::ARRAY_INTO_ITERATOR) =>
|
if span.is_desugaring(DesugaringKind::ForLoop) && self.msrv.meets(msrvs::ARRAY_INTO_ITERATOR) =>
|
||||||
{
|
{
|
||||||
// report the error around the `vec!` not inside `<std macros>:`
|
let suggest_slice = suggest_type(expr);
|
||||||
let span = expr.span.ctxt().outer_expn_data().call_site;
|
self.check_vec_macro(cx, &vec_args, callsite, expr.hir_id, suggest_slice);
|
||||||
self.check_vec_macro(cx, &vec_args, span, expr.hir_id, SuggestedType::Array);
|
|
||||||
},
|
},
|
||||||
// search for `&vec![_]` or `vec![_]` expressions where the adjusted type is `&[_]`
|
// search for `&vec![_]` or `vec![_]` expressions where the adjusted type is `&[_]`
|
||||||
_ => {
|
_ => {
|
||||||
let (suggest_slice, span) = if let ExprKind::AddrOf(BorrowKind::Ref, mutability, _) = expr.kind {
|
let suggest_slice = suggest_type(expr);
|
||||||
// `expr` is `&vec![_]`, so suggest `&[_]` (or `&mut[_]` resp.)
|
|
||||||
(SuggestedType::SliceRef(mutability), expr.span)
|
|
||||||
} else {
|
|
||||||
// `expr` is the `vec![_]` expansion, so suggest `[_]`
|
|
||||||
// and also use the span of the actual `vec![_]` expression
|
|
||||||
(SuggestedType::Array, expr.span.ctxt().outer_expn_data().call_site)
|
|
||||||
};
|
|
||||||
|
|
||||||
if adjusts_to_slice(cx, expr) {
|
if adjusts_to_slice(cx, expr) {
|
||||||
self.check_vec_macro(cx, &vec_args, span, expr.hir_id, suggest_slice);
|
self.check_vec_macro(cx, &vec_args, callsite, expr.hir_id, suggest_slice);
|
||||||
} else {
|
} else {
|
||||||
self.span_to_lint_map.insert(span, None);
|
self.span_to_lint_map.insert(callsite, None);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -151,8 +143,6 @@ fn check_vec_macro<'tcx>(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut applicability = Applicability::MachineApplicable;
|
|
||||||
|
|
||||||
let snippet = match *vec_args {
|
let snippet = match *vec_args {
|
||||||
higher::VecArgs::Repeat(elem, len) => {
|
higher::VecArgs::Repeat(elem, len) => {
|
||||||
if let Some(Constant::Int(len_constant)) = constant(cx, cx.typeck_results(), len) {
|
if let Some(Constant::Int(len_constant)) = constant(cx, cx.typeck_results(), len) {
|
||||||
@ -166,7 +156,7 @@ fn check_vec_macro<'tcx>(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
suggest_slice.snippet(cx, Some(elem.span), Some(len.span), &mut applicability)
|
suggest_slice.snippet(cx, Some(elem.span), Some(len.span))
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -180,13 +170,16 @@ fn check_vec_macro<'tcx>(
|
|||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
suggest_slice.snippet(cx, args_span, None, &mut applicability)
|
suggest_slice.snippet(cx, args_span, None)
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
self.span_to_lint_map
|
self.span_to_lint_map.entry(span).or_insert(Some((
|
||||||
.entry(span)
|
hir_id,
|
||||||
.or_insert(Some((hir_id, suggest_slice, snippet, applicability)));
|
suggest_slice,
|
||||||
|
snippet,
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,24 +199,16 @@ fn desc(self) -> &'static str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn snippet(
|
fn snippet(self, cx: &LateContext<'_>, args_span: Option<Span>, len_span: Option<Span>) -> String {
|
||||||
self,
|
let maybe_args = args_span.and_then(|sp| snippet_opt(cx, sp)).unwrap_or_default();
|
||||||
cx: &LateContext<'_>,
|
|
||||||
args_span: Option<Span>,
|
|
||||||
len_span: Option<Span>,
|
|
||||||
app: &mut Applicability,
|
|
||||||
) -> String {
|
|
||||||
let args = args_span
|
|
||||||
.map(|sp| snippet_with_applicability(cx, sp, "..", app))
|
|
||||||
.unwrap_or_default();
|
|
||||||
let maybe_len = len_span
|
let maybe_len = len_span
|
||||||
.map(|sp| format!("; {}", snippet_with_applicability(cx, sp, "len", app)))
|
.and_then(|sp| snippet_opt(cx, sp).map(|s| format!("; {s}")))
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
match self {
|
match self {
|
||||||
Self::SliceRef(Mutability::Mut) => format!("&mut [{args}{maybe_len}]"),
|
Self::SliceRef(Mutability::Mut) => format!("&mut [{maybe_args}{maybe_len}]"),
|
||||||
Self::SliceRef(Mutability::Not) => format!("&[{args}{maybe_len}]"),
|
Self::SliceRef(Mutability::Not) => format!("&[{maybe_args}{maybe_len}]"),
|
||||||
Self::Array => format!("[{args}{maybe_len}]"),
|
Self::Array => format!("[{maybe_args}{maybe_len}]"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -249,3 +234,13 @@ pub fn is_allowed_vec_method(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
|
|||||||
is_trait_method(cx, e, sym::IntoIterator)
|
is_trait_method(cx, e, sym::IntoIterator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn suggest_type(expr: &Expr<'_>) -> SuggestedType {
|
||||||
|
if let ExprKind::AddrOf(BorrowKind::Ref, mutability, _) = expr.kind {
|
||||||
|
// `expr` is `&vec![_]`, so suggest `&[_]` (or `&mut[_]` resp.)
|
||||||
|
SuggestedType::SliceRef(mutability)
|
||||||
|
} else {
|
||||||
|
// `expr` is the `vec![_]` expansion, so suggest `[_]`
|
||||||
|
SuggestedType::Array
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -217,3 +217,7 @@ fn issue_11958() {
|
|||||||
// should not lint, `String` is not `Copy`
|
// should not lint, `String` is not `Copy`
|
||||||
f(&vec!["test".to_owned(); 2]);
|
f(&vec!["test".to_owned(); 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue_12101() {
|
||||||
|
for a in &[1, 2] {}
|
||||||
|
}
|
||||||
|
@ -217,3 +217,7 @@ fn f(_s: &[String]) {}
|
|||||||
// should not lint, `String` is not `Copy`
|
// should not lint, `String` is not `Copy`
|
||||||
f(&vec!["test".to_owned(); 2]);
|
f(&vec!["test".to_owned(); 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue_12101() {
|
||||||
|
for a in &(vec![1, 2]) {}
|
||||||
|
}
|
||||||
|
@ -121,5 +121,11 @@ error: useless use of `vec!`
|
|||||||
LL | this_macro_doesnt_need_vec!(vec![1]);
|
LL | this_macro_doesnt_need_vec!(vec![1]);
|
||||||
| ^^^^^^^ help: you can use an array directly: `[1]`
|
| ^^^^^^^ help: you can use an array directly: `[1]`
|
||||||
|
|
||||||
error: aborting due to 20 previous errors
|
error: useless use of `vec!`
|
||||||
|
--> tests/ui/vec.rs:222:14
|
||||||
|
|
|
||||||
|
LL | for a in &(vec![1, 2]) {}
|
||||||
|
| ^^^^^^^^^^^^^ help: you can use a slice directly: `&[1, 2]`
|
||||||
|
|
||||||
|
error: aborting due to 21 previous errors
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user