2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-10-05 15:43:44 +00:00
|
|
|
use clippy_utils::higher::{get_vec_init_kind, VecInitKind};
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::source::snippet;
|
2021-10-05 15:43:44 +00:00
|
|
|
use clippy_utils::{path_to_local, path_to_local_id};
|
2021-01-15 09:56:44 +00:00
|
|
|
use if_chain::if_chain;
|
|
|
|
use rustc_errors::Applicability;
|
2021-10-03 04:23:57 +00:00
|
|
|
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, Local, PatKind, Stmt, StmtKind};
|
2021-01-15 09:56:44 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
|
|
|
use rustc_middle::lint::in_external_macro;
|
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-10-03 04:23:57 +00:00
|
|
|
use rustc_span::Span;
|
2021-01-15 09:56:44 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for calls to `push` immediately after creating a new `Vec`.
|
2021-01-15 09:56:44 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// The `vec![]` macro is both more performant and easier to read than
|
2021-01-15 09:56:44 +00:00
|
|
|
/// multiple `push` calls.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2021-01-15 09:56:44 +00:00
|
|
|
/// ```rust
|
|
|
|
/// let mut v = Vec::new();
|
|
|
|
/// v.push(0);
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// let v = vec![0];
|
|
|
|
/// ```
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 19:06:26 +00:00
|
|
|
#[clippy::version = "1.51.0"]
|
2021-01-15 09:56:44 +00:00
|
|
|
pub VEC_INIT_THEN_PUSH,
|
|
|
|
perf,
|
|
|
|
"`push` immediately after `Vec` creation"
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(VecInitThenPush => [VEC_INIT_THEN_PUSH]);
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct VecInitThenPush {
|
|
|
|
searcher: Option<VecPushSearcher>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct VecPushSearcher {
|
2021-02-25 10:25:22 +00:00
|
|
|
local_id: HirId,
|
2021-01-15 09:56:44 +00:00
|
|
|
init: VecInitKind,
|
|
|
|
lhs_is_local: bool,
|
|
|
|
lhs_span: Span,
|
|
|
|
err_span: Span,
|
|
|
|
found: u64,
|
|
|
|
}
|
|
|
|
impl VecPushSearcher {
|
|
|
|
fn display_err(&self, cx: &LateContext<'_>) {
|
|
|
|
match self.init {
|
|
|
|
_ if self.found == 0 => return,
|
2021-10-03 04:23:57 +00:00
|
|
|
VecInitKind::WithLiteralCapacity(x) if x > self.found => return,
|
|
|
|
VecInitKind::WithExprCapacity(_) => return,
|
2021-01-15 09:56:44 +00:00
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut s = if self.lhs_is_local {
|
|
|
|
String::from("let ")
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
s.push_str(&snippet(cx, self.lhs_span, ".."));
|
|
|
|
s.push_str(" = vec![..];");
|
|
|
|
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
VEC_INIT_THEN_PUSH,
|
|
|
|
self.err_span,
|
|
|
|
"calls to `push` immediately after creation",
|
|
|
|
"consider using the `vec![]` macro",
|
|
|
|
s,
|
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LateLintPass<'_> for VecInitThenPush {
|
2021-02-25 10:25:22 +00:00
|
|
|
fn check_block(&mut self, _: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
|
2021-01-15 09:56:44 +00:00
|
|
|
self.searcher = None;
|
2021-02-25 10:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
|
2021-01-15 09:56:44 +00:00
|
|
|
if_chain! {
|
|
|
|
if !in_external_macro(cx.sess(), local.span);
|
|
|
|
if let Some(init) = local.init;
|
2021-02-25 10:25:22 +00:00
|
|
|
if let PatKind::Binding(BindingAnnotation::Mutable, id, _, None) = local.pat.kind;
|
2021-01-15 09:56:44 +00:00
|
|
|
if let Some(init_kind) = get_vec_init_kind(cx, init);
|
|
|
|
then {
|
|
|
|
self.searcher = Some(VecPushSearcher {
|
2021-02-25 10:25:22 +00:00
|
|
|
local_id: id,
|
2021-01-15 09:56:44 +00:00
|
|
|
init: init_kind,
|
|
|
|
lhs_is_local: true,
|
|
|
|
lhs_span: local.ty.map_or(local.pat.span, |t| local.pat.span.to(t.span)),
|
|
|
|
err_span: local.span,
|
|
|
|
found: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2021-04-08 15:50:13 +00:00
|
|
|
if_chain! {
|
|
|
|
if self.searcher.is_none();
|
|
|
|
if !in_external_macro(cx.sess(), expr.span);
|
|
|
|
if let ExprKind::Assign(left, right, _) = expr.kind;
|
|
|
|
if let Some(id) = path_to_local(left);
|
|
|
|
if let Some(init_kind) = get_vec_init_kind(cx, right);
|
|
|
|
then {
|
|
|
|
self.searcher = Some(VecPushSearcher {
|
|
|
|
local_id: id,
|
|
|
|
init: init_kind,
|
|
|
|
lhs_is_local: false,
|
|
|
|
lhs_span: left.span,
|
|
|
|
err_span: expr.span,
|
|
|
|
found: 0,
|
|
|
|
});
|
2021-01-15 09:56:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
|
|
|
|
if let Some(searcher) = self.searcher.take() {
|
|
|
|
if_chain! {
|
|
|
|
if let StmtKind::Expr(expr) | StmtKind::Semi(expr) = stmt.kind;
|
|
|
|
if let ExprKind::MethodCall(path, _, [self_arg, _], _) = expr.kind;
|
2021-02-25 10:25:22 +00:00
|
|
|
if path_to_local_id(self_arg, searcher.local_id);
|
2021-01-15 09:56:44 +00:00
|
|
|
if path.ident.name.as_str() == "push";
|
|
|
|
then {
|
|
|
|
self.searcher = Some(VecPushSearcher {
|
|
|
|
found: searcher.found + 1,
|
|
|
|
err_span: searcher.err_span.to(stmt.span),
|
|
|
|
.. searcher
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
searcher.display_err(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_block_post(&mut self, cx: &LateContext<'tcx>, _: &'tcx Block<'tcx>) {
|
|
|
|
if let Some(searcher) = self.searcher.take() {
|
|
|
|
searcher.display_err(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|