-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Suggest usage of array and Vec .as_slice()
and .as_mut_slice()
methods
#8405
Closed
Closed
Changes from 4 commits
Commits
Show all changes
5 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 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,86 @@ | ||
use clippy_utils::{ | ||
diagnostics::span_lint_and_sugg, meets_msrv, msrvs, source::snippet_with_context, ty::is_type_lang_item, | ||
}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{lang_items, BorrowKind, Expr, ExprKind, Mutability}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_semver::RustcVersion; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Finds arrays or vectors being converted to slices of the same length. | ||
/// ### Why is this bad? | ||
/// The methods `as_slice()` or `as_mut_slice()` could be used instead. | ||
/// ### Example | ||
/// ```rust | ||
/// let mut arr: [u32; 3] = [1, 2, 3]; | ||
/// let arr_slice = &arr[..]; | ||
/// let mutable_arr_slice = &mut arr[..]; | ||
/// | ||
/// let mut vec = vec![1, 2, 3]; | ||
/// let vec_slice = &vec[..]; | ||
/// let mutable_vec_slice = &mut vec[..]; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// let mut arr: [u32; 3] = [1, 2, 3]; | ||
/// let arr_slice = arr.as_slice(); | ||
/// let mutable_arr_slice = arr.as_mut_slice(); | ||
/// | ||
/// let mut vec = vec![1, 2, 3]; | ||
/// let vec_slice = vec.as_slice(); | ||
/// let mutable_vec_slice = vec.as_mut_slice(); | ||
/// ``` | ||
#[clippy::version = "1.60.0"] | ||
pub MANUAL_SLICE, | ||
restriction, | ||
"Suggest use of array and Vec .as_slice() and .as_mut_slice() methods" | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct ManualSlice { | ||
msrv: Option<RustcVersion>, | ||
} | ||
|
||
impl ManualSlice { | ||
#[must_use] | ||
pub fn new(msrv: Option<RustcVersion>) -> Self { | ||
Self { msrv } | ||
} | ||
} | ||
|
||
impl_lint_pass!(ManualSlice => [MANUAL_SLICE]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for ManualSlice { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { | ||
if !meets_msrv(self.msrv.as_ref(), &msrvs::NON_EXHAUSTIVE) { | ||
return; | ||
} | ||
let ctxt = expr.span.ctxt(); | ||
if_chain! { | ||
if let ExprKind::AddrOf(BorrowKind::Ref, mutability, inner) = &expr.kind; | ||
if let ExprKind::Index(object, range) = &inner.kind; | ||
if is_type_lang_item(cx, cx.typeck_results().expr_ty_adjusted(range), lang_items::LangItem::RangeFull); | ||
then { | ||
let mut app = Applicability::MachineApplicable; | ||
let snip = snippet_with_context(cx, object.span, ctxt, "..", &mut app).0; | ||
let suggested_method = match mutability { | ||
Mutability::Not => "to_slice()", | ||
Mutability::Mut => "to_mut_slice()", | ||
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. Should be |
||
}; | ||
span_lint_and_sugg( | ||
cx, | ||
MANUAL_SLICE, | ||
expr.span, | ||
"converting to a slice of the same length", | ||
"use", | ||
format!("{}.{}", snip, suggested_method), | ||
app | ||
); | ||
} | ||
} | ||
} | ||
|
||
extract_msrv_attr!(LateContext); | ||
} |
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,29 @@ | ||
#![warn(clippy::manual_slice)] | ||
|
||
fn main() { | ||
let mut arr: [u32; 3] = [1, 2, 3]; | ||
|
||
let arr_slice = &arr[..]; | ||
let mutable_arr_slice = &mut arr[..]; | ||
|
||
let mut vec = vec![1, 2, 3]; | ||
|
||
let vec_slice = &vec[..]; | ||
let mutable_vec_slice = &mut vec[..]; | ||
|
||
// Will not fire on any of these | ||
|
||
let partial_slice_1 = &arr[1..]; | ||
let partial_slice_2 = &arr[..3]; | ||
let partial_slice_3 = &arr[1..3]; | ||
let partial_mut_slice_1 = &mut arr[1..]; | ||
let partial_mut_slice_2 = &mut arr[..3]; | ||
let partial_mut_slice_3 = &mut arr[1..3]; | ||
|
||
let partial_slice_1 = &vec[1..]; | ||
let partial_slice_2 = &vec[..3]; | ||
let partial_slice_3 = &vec[1..3]; | ||
let partial_mut_slice_1 = &mut vec[1..]; | ||
let partial_mut_slice_2 = &mut vec[..3]; | ||
let partial_mut_slice_3 = &mut vec[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,28 @@ | ||
error: converting to a slice of the same length | ||
--> $DIR/manual_slice.rs:6:21 | ||
| | ||
LL | let arr_slice = &arr[..]; | ||
| ^^^^^^^^ help: use: `arr.to_slice()` | ||
| | ||
= note: `-D clippy::manual-slice` implied by `-D warnings` | ||
|
||
error: converting to a slice of the same length | ||
--> $DIR/manual_slice.rs:7:29 | ||
| | ||
LL | let mutable_arr_slice = &mut arr[..]; | ||
| ^^^^^^^^^^^^ help: use: `arr.to_mut_slice()` | ||
|
||
error: converting to a slice of the same length | ||
--> $DIR/manual_slice.rs:11:21 | ||
| | ||
LL | let vec_slice = &vec[..]; | ||
| ^^^^^^^^ help: use: `vec.to_slice()` | ||
|
||
error: converting to a slice of the same length | ||
--> $DIR/manual_slice.rs:12:29 | ||
| | ||
LL | let mutable_vec_slice = &mut vec[..]; | ||
| ^^^^^^^^^^^^ help: use: `vec.to_mut_slice()` | ||
|
||
error: aborting due to 4 previous errors | ||
|
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.
not sure about this.