auto-fix slow_vector_initialization
This commit is contained in:
parent
7a01033797
commit
39269aaaae
3 changed files with 96 additions and 2 deletions
|
|
@ -3,6 +3,7 @@ use clippy_utils::macros::matching_root_macro_call;
|
|||
use clippy_utils::sugg::Sugg;
|
||||
use clippy_utils::{
|
||||
SpanlessEq, get_enclosing_block, is_integer_literal, is_path_diagnostic_item, path_to_local, path_to_local_id,
|
||||
span_contains_comment,
|
||||
};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::intravisit::{Visitor, walk_block, walk_expr, walk_stmt};
|
||||
|
|
@ -206,6 +207,14 @@ impl SlowVectorInit {
|
|||
let span_to_replace = slow_fill
|
||||
.span
|
||||
.with_lo(vec_alloc.allocation_expr.span.source_callsite().lo());
|
||||
|
||||
// If there is no comment in `span_to_replace`, Clippy can automatically fix the code.
|
||||
let app = if span_contains_comment(cx.tcx.sess.source_map(), span_to_replace) {
|
||||
Applicability::Unspecified
|
||||
} else {
|
||||
Applicability::MachineApplicable
|
||||
};
|
||||
|
||||
span_lint_and_sugg(
|
||||
cx,
|
||||
SLOW_VECTOR_INITIALIZATION,
|
||||
|
|
@ -213,7 +222,7 @@ impl SlowVectorInit {
|
|||
msg,
|
||||
"consider replacing this with",
|
||||
format!("vec![0; {len_expr}]"),
|
||||
Applicability::Unspecified,
|
||||
app,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue