Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
hotfix: prevent linting raw int
  • Loading branch information
chansuke committed Apr 11, 2023
commit 37fd6b3ddf1a8c4d35bd599d3248bd8f0beaf9dc
8 changes: 6 additions & 2 deletions clippy_lints/src/unnecessary_reserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryReserve {
return;
}

if let ExprKind::MethodCall(PathSegment { ident: method, .. }, struct_calling_on, _, _) = expr.kind
if let ExprKind::MethodCall(PathSegment { ident: method, .. }, struct_calling_on, args_a, _) = expr.kind
&& method.name.as_str() == "reserve"
&& acceptable_type(cx, struct_calling_on)
&& let Some(block) = get_enclosing_block(cx, expr.hir_id)
&& let Some(next_stmt_span) = check_extend_method(cx, block, struct_calling_on)
&& let Some(next_stmt_span) = check_extend_method(cx, block, struct_calling_on, &args_a[0])
&& !next_stmt_span.from_expansion()
{
span_lint_and_then(
Expand Down Expand Up @@ -95,13 +95,17 @@ fn check_extend_method(
cx: &LateContext<'_>,
block: &Block<'_>,
struct_expr: &rustc_hir::Expr<'_>,
args_a: &rustc_hir::Expr<'_>,
) -> Option<rustc_span::Span> {
let args_a_kind = &args_a.kind;
let mut read_found = false;
let mut spanless_eq = SpanlessEq::new(cx);

let _: Option<!> = for_each_expr(block, |expr| {
if let ExprKind::MethodCall(_, struct_calling_on, _,_) = expr.kind
&& let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
&& let ExprKind::MethodCall(PathSegment { ident: method_call_a, .. },..) = args_a_kind
&& method_call_a.name.as_str() == "len"
&& match_def_path(cx, expr_def_id, &paths::ITER_EXTEND)
&& acceptable_type(cx, struct_calling_on)
// Check that both expr are equal
Expand Down
64 changes: 3 additions & 61 deletions tests/ui/unnecessary_reserve.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
error: unnecessary call to `reserve`
--> $DIR/unnecessary_reserve.rs:14:18
|
LL | fn vec_reserve() {
| __________________^
LL | | let mut vec: Vec<usize> = vec![];
LL | | let array: &[usize] = &[1, 2];
LL | |
LL | | // do not lint
LL | | vec.reserve(1);
| | -------------- help: remove this line
... |
LL | | vec.extend([1])
LL | | }
| |_^
|
= note: `-D clippy::unnecessary-reserve` implied by `-D warnings`

error: unnecessary call to `reserve`
--> $DIR/unnecessary_reserve.rs:14:18
|
Expand All @@ -31,6 +13,8 @@ LL | | vec.reserve(array.len());
LL | | vec.extend([1])
LL | | }
| |_^
|
= note: `-D clippy::unnecessary-reserve` implied by `-D warnings`

error: unnecessary call to `reserve`
--> $DIR/unnecessary_reserve.rs:27:5
Expand Down Expand Up @@ -58,22 +42,6 @@ LL | | vec.extend([1])
LL | | }
| |_^

error: unnecessary call to `reserve`
--> $DIR/unnecessary_reserve.rs:43:24
|
LL | fn vec_deque_reserve() {
| ________________________^
LL | | let mut vec_deque: VecDeque<usize> = [1].into();
LL | | let array: &[usize] = &[1, 2];
LL | |
LL | | // do not lint
LL | | vec_deque.reserve(1);
| | -------------------- help: remove this line
... |
LL | | vec_deque.extend([1])
LL | | }
| |_^

error: unnecessary call to `reserve`
--> $DIR/unnecessary_reserve.rs:43:24
|
Expand All @@ -90,31 +58,5 @@ LL | | vec_deque.extend([1])
LL | | }
| |_^

error: unnecessary call to `reserve`
--> $DIR/unnecessary_reserve.rs:56:5
|
LL | / {
LL | | vec_deque.reserve(1);
| | -------------------- help: remove this line
LL | | vec_deque.extend([1])
LL | | };
| |_____^

error: unnecessary call to `reserve`
--> $DIR/unnecessary_reserve.rs:43:24
|
LL | fn vec_deque_reserve() {
| ________________________^
LL | | let mut vec_deque: VecDeque<usize> = [1].into();
LL | | let array: &[usize] = &[1, 2];
LL | |
... |
LL | | vec_deque.reserve(array.len() + 1);
| | ---------------------------------- help: remove this line
... |
LL | | vec_deque.extend([1])
LL | | }
| |_^

error: aborting due to 8 previous errors
error: aborting due to 4 previous errors