Skip to content

Commit

Permalink
updated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Valentine-Mario committed May 26, 2021
1 parent 6d50cff commit 64341cc
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2295,6 +2295,7 @@ Released 2018-09-13
<!-- begin autogenerated links to lint list -->
[`absurd_extreme_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons
[`almost_swapped`]: https://rust-lang.github.io/rust-clippy/master/index.html#almost_swapped
[`append_instead_of_extend`]: https://rust-lang.github.io/rust-clippy/master/index.html#append_instead_of_extend
[`approx_constant`]: https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant
[`as_conversions`]: https://rust-lang.github.io/rust-clippy/master/index.html#as_conversions
[`assertions_on_constants`]: https://rust-lang.github.io/rust-clippy/master/index.html#assertions_on_constants
Expand Down
61 changes: 61 additions & 0 deletions clippy_lints/src/append_instead_of_extend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use rustc_session::{declare_lint_pass, declare_tool_lint};
// use rustc_ast::ast::*;
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::ty::is_type_diagnostic_item;
use if_chain::if_chain;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_span::symbol::sym;

declare_clippy_lint! {
/// **What it does:**
///
/// **Why is this bad?**
///
/// **Known problems:** None.
///
/// **Example:**
///
/// ```rust
/// // example code where clippy issues a warning
/// ```
/// Use instead:
/// ```rust
/// // example code which does not raise clippy warning
/// ```
pub APPEND_INSTEAD_OF_EXTEND,
perf,
"default lint description"
}

declare_lint_pass!(AppendInsteadOfExtend => [APPEND_INSTEAD_OF_EXTEND]);

impl<'tcx> LateLintPass<'tcx> for AppendInsteadOfExtend {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
if is_vec_an_append(cx, expr) {
span_lint_and_help(
cx,
APPEND_INSTEAD_OF_EXTEND,
expr.span,
"use of `vec.extend(other_vec.drain(..))`",
None,
"consider using `vec![].append(&mut vec![]);` instead",
);
}
}
}

fn is_vec_an_append<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> bool {
if_chain! {
if let ExprKind::MethodCall(method_name, _, exprs, _) = expr.kind;
if method_name.ident.as_str() == "extend";
let ty = cx.typeck_results().expr_ty(&exprs[0]).peel_refs();
if is_type_diagnostic_item(cx, ty, sym::vec_type);
if let ExprKind::MethodCall(method_name2, _, _exprs2, _) = &exprs[1].kind;
if method_name2.ident.as_str() == "drain";
then {
return true
}
}
false
}
6 changes: 5 additions & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ mod deprecated_lints;

// begin lints modules, do not remove this comment, it’s used in `update_lints`
mod absurd_extreme_comparisons;
mod append_instead_of_extend;
mod approx_const;
mod arithmetic;
mod as_conversions;
Expand Down Expand Up @@ -534,6 +535,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
#[cfg(feature = "internal-lints")]
utils::internal_lints::UNNECESSARY_SYMBOL_STR,
absurd_extreme_comparisons::ABSURD_EXTREME_COMPARISONS,
append_instead_of_extend::APPEND_INSTEAD_OF_EXTEND,
approx_const::APPROX_CONSTANT,
arithmetic::FLOAT_ARITHMETIC,
arithmetic::INTEGER_ARITHMETIC,
Expand Down Expand Up @@ -1166,6 +1168,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:

store.register_group(true, "clippy::all", Some("clippy"), vec![
LintId::of(absurd_extreme_comparisons::ABSURD_EXTREME_COMPARISONS),
LintId::of(append_instead_of_extend::APPEND_INSTEAD_OF_EXTEND),
LintId::of(approx_const::APPROX_CONSTANT),
LintId::of(assertions_on_constants::ASSERTIONS_ON_CONSTANTS),
LintId::of(assign_ops::ASSIGN_OP_PATTERN),
Expand Down Expand Up @@ -1744,6 +1747,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
]);

store.register_group(true, "clippy::perf", Some("clippy_perf"), vec![
LintId::of(append_instead_of_extend::APPEND_INSTEAD_OF_EXTEND),
LintId::of(entry::MAP_ENTRY),
LintId::of(escape::BOXED_LOCAL),
LintId::of(large_const_arrays::LARGE_CONST_ARRAYS),
Expand Down Expand Up @@ -2078,7 +2082,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(move || box if_then_some_else_none::IfThenSomeElseNone::new(msrv));
store.register_early_pass(|| box bool_assert_comparison::BoolAssertComparison);
store.register_late_pass(|| box unused_async::UnusedAsync);

store.register_late_pass(|| box append_instead_of_extend::AppendInsteadOfExtend);
}

#[rustfmt::skip]
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/append_instead_of_extend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![warn(clippy::append_instead_of_extend)]

fn main() {
let mut a = vec![0u8; 1024];
let mut b = Vec::new();

b.extend(a.drain(..));

let mut c = vec![0u8, 10];
let mut d: std::vec::Vec<u8> = Vec::new();

c.extend(d.drain(1..3))
}
11 changes: 11 additions & 0 deletions tests/ui/append_instead_of_extend.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: use of `vec.extend(other_vec.drain(..))`
--> $DIR/append_instead_of_extend.rs:7:5
|
LL | b.extend(a.drain(..));
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::append-instead-of-extend` implied by `-D warnings`
= help: consider using `vec![].append(&mut vec![]);` instead

error: aborting due to previous error

0 comments on commit 64341cc

Please sign in to comment.