-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6d50cff
commit 64341cc
Showing
5 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|