-
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.
Move
VecResizeToZero
into Methods
lint pass
- Loading branch information
Showing
9 changed files
with
88 additions
and
78 deletions.
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
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
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,45 @@ | ||
use clippy_utils::diagnostics::span_lint_and_then; | ||
use clippy_utils::ty::is_type_diagnostic_item; | ||
use if_chain::if_chain; | ||
use rustc_ast::LitKind; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_span::source_map::Spanned; | ||
use rustc_span::{sym, Span}; | ||
|
||
use super::VEC_RESIZE_TO_ZERO; | ||
|
||
pub(super) fn check<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx Expr<'_>, | ||
count_arg: &'tcx Expr<'_>, | ||
default_arg: &'tcx Expr<'_>, | ||
name_span: Span, | ||
) { | ||
if_chain! { | ||
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); | ||
if let Some(impl_id) = cx.tcx.impl_of_method(method_id); | ||
if is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id), sym::Vec); | ||
if let ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = count_arg.kind; | ||
if let ExprKind::Lit(Spanned { node: LitKind::Int(..), .. }) = default_arg.kind; | ||
then { | ||
let method_call_span = expr.span.with_lo(name_span.lo()); | ||
span_lint_and_then( | ||
cx, | ||
VEC_RESIZE_TO_ZERO, | ||
expr.span, | ||
"emptying a vector with `resize`", | ||
|db| { | ||
db.help("the arguments may be inverted..."); | ||
db.span_suggestion( | ||
method_call_span, | ||
"...or you can empty the vector with", | ||
"clear()".to_string(), | ||
Applicability::MaybeIncorrect, | ||
); | ||
}, | ||
); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,15 +1,19 @@ | ||
#![warn(clippy::vec_resize_to_zero)] | ||
|
||
fn main() { | ||
let mut v = vec![1, 2, 3, 4, 5]; | ||
|
||
// applicable here | ||
vec![1, 2, 3, 4, 5].resize(0, 5); | ||
v.resize(0, 5); | ||
|
||
// not applicable | ||
vec![1, 2, 3, 4, 5].resize(2, 5); | ||
v.resize(2, 5); | ||
|
||
let mut v = vec!["foo", "bar", "baz"]; | ||
|
||
// applicable here, but only implemented for integer literals for now | ||
vec!["foo", "bar", "baz"].resize(0, "bar"); | ||
v.resize(0, "bar"); | ||
|
||
// not applicable | ||
vec!["foo", "bar", "baz"].resize(2, "bar") | ||
v.resize(2, "bar") | ||
} |
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