-
Notifications
You must be signed in to change notification settings - Fork 1.7k
WIP: add unnecessary_split_off lint #14814
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sebhernandezr
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
sebhernandezr:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::sym; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::declare_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Suggests using `drain(..).collect()` when a `split_off(0)` is being called on a `vec`. | ||
/// ### Why is this bad? | ||
/// Because splitting implies dividing the vec into two parts, so the modified vector being emptied could be unexpected. | ||
/// ### Example | ||
/// ```no_run | ||
/// let mut vec = vec![1, 2, 3]; | ||
/// let vec1 = vec.split_off(0); | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// let mut vec = vec![1, 2, 3]; | ||
/// let vec1 = vec.drain(..).collect(); | ||
/// ``` | ||
#[clippy::version = "1.88.0"] | ||
pub UNNECESSARY_SPLIT_OFF, | ||
style, | ||
"unnecessary `split_off(0)`" | ||
} | ||
declare_lint_pass!(UnnecessarySplitOff => [UNNECESSARY_SPLIT_OFF]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for UnnecessarySplitOff { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) { | ||
if let ExprKind::MethodCall(path, value, args, span) = &expr.kind | ||
// FIXME: sym::split_off does not exist, but this still triggers the lint to use it. | ||
&& path.ident.name.as_str() == "split_off" | ||
{ | ||
let ty = cx.typeck_results().expr_ty(value); | ||
if clippy_utils::ty::is_type_diagnostic_item(cx, ty, sym::Vec) { | ||
let &[arg] = args else { | ||
return; | ||
}; | ||
if clippy_utils::is_integer_literal(arg, 0) || clippy_utils::is_integer_const(cx, arg, 0) { | ||
span_lint_and_sugg( | ||
cx, | ||
UNNECESSARY_SPLIT_OFF, | ||
*span, | ||
"unnecessary `split_off(0)`", | ||
"use", | ||
"drain(..).collect()".to_string(), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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,28 @@ | ||
//@no-rustfix | ||
#![warn(clippy::unnecessary_split_off)] | ||
#![allow(unused)] | ||
|
||
struct A; | ||
impl A { | ||
fn split_off(&mut self, _: usize) {} | ||
} | ||
|
||
const ZERO: usize = 0; | ||
|
||
fn main() { | ||
let mut vec1 = vec![1, 2, 3]; | ||
|
||
let vec2: Vec<_> = vec1.split_off(0); | ||
//~^ unnecessary_split_off | ||
|
||
let vec3: Vec<_> = vec1.split_off(1); | ||
|
||
let vec4: Vec<_> = vec1.split_off(ZERO); | ||
//~^ unnecessary_split_off | ||
|
||
let vec5: Vec<_> = vec1.split_off(const { 0 }); | ||
//~^ unnecessary_split_off | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd also like to test let zero = 0;
let vec6 = vec1.split_off(zero); |
||
let mut a = A; | ||
a.split_off(0); | ||
} |
This file contains hidden or 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,23 @@ | ||
error: unnecessary `split_off(0)` | ||
--> tests/ui/unnecessary_split_off.rs:15:29 | ||
| | ||
LL | let vec2: Vec<_> = vec1.split_off(0); | ||
| ^^^^^^^^^^^^ help: use: `drain(..).collect()` | ||
| | ||
= note: `-D clippy::unnecessary-split-off` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_split_off)]` | ||
|
||
error: unnecessary `split_off(0)` | ||
--> tests/ui/unnecessary_split_off.rs:20:29 | ||
| | ||
LL | let vec4: Vec<_> = vec1.split_off(ZERO); | ||
| ^^^^^^^^^^^^^^^ help: use: `drain(..).collect()` | ||
|
||
error: unnecessary `split_off(0)` | ||
--> tests/ui/unnecessary_split_off.rs:23:29 | ||
| | ||
LL | let vec5: Vec<_> = vec1.split_off(const { 0 }); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ help: use: `drain(..).collect()` | ||
|
||
error: aborting due to 3 previous errors | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latter should be enough. Each literal is also a const