-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add manual_as_slice lint
#14809
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
Open
nils-degroot
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
nils-degroot: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.
Open
Add manual_as_slice lint
#14809
Changes from all commits
Commits
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,66 @@ | ||
| use clippy_utils::diagnostics::span_lint_and_then; | ||
| use rustc_errors::Applicability; | ||
| use rustc_hir::{Expr, ExprKind, LangItem, Mutability, QPath}; | ||
| use rustc_lint::{LateContext, LateLintPass}; | ||
| use rustc_middle::ty::TyKind; | ||
| use rustc_session::declare_lint_pass; | ||
| use rustc_span::symbol::sym; | ||
|
|
||
| declare_clippy_lint! { | ||
| /// ### What it does | ||
| /// | ||
| /// Detects if a full range slice reference is used instead of using the `.as_slice()` method. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// | ||
| /// Using the `some_value.as_slice()` method is more explicit then using `&some_value[..]` | ||
| /// | ||
| /// ### Example | ||
| /// ```no_run | ||
| /// let array: [u8; 4] = [0; 4]; | ||
| /// let slice = &array[..]; | ||
| /// ``` | ||
| /// Use instead: | ||
| /// ```no_run | ||
| /// let array: [u8; 4] = [0; 4]; | ||
| /// let slice = array.as_slice(); | ||
| /// ``` | ||
| #[clippy::version = "1.88.0"] | ||
| pub MANUAL_AS_SLICE, | ||
| nursery, | ||
| "Use as slice instead of borrow full range." | ||
| } | ||
| declare_lint_pass!(ManualAsSlice => [MANUAL_AS_SLICE]); | ||
|
|
||
| impl LateLintPass<'_> for ManualAsSlice { | ||
| fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
| if let ExprKind::AddrOf(_, mutability, borrow) = expr.kind | ||
| && let ExprKind::Index(value, index, index_span) = borrow.kind | ||
| && let ExprKind::Struct(qpath, _, _) = index.kind | ||
| && let QPath::LangItem(LangItem::RangeFull, _) = qpath | ||
| { | ||
| let sugg_tail = match mutability { | ||
| Mutability::Not => ".as_slice()", | ||
| Mutability::Mut => ".as_mut_slice()", | ||
| }; | ||
|
|
||
| let borrow_span = expr.span.until(borrow.span); | ||
| let app = Applicability::MachineApplicable; | ||
|
|
||
| match cx.typeck_results().expr_ty(value).kind() { | ||
| TyKind::Array(_, _) | TyKind::Slice(_) => {}, | ||
| TyKind::Ref(_, t, _) if let TyKind::Array(_, _) | TyKind::Slice(_) = t.kind() => {}, | ||
| TyKind::Adt(adt, _) if cx.tcx.is_diagnostic_item(sym::Vec, adt.did()) => {}, | ||
| _ => return, | ||
| } | ||
|
|
||
| span_lint_and_then(cx, MANUAL_AS_SLICE, expr.span, "using a full range slice", |diag| { | ||
| diag.multipart_suggestion( | ||
| "try", | ||
| vec![(borrow_span, String::new()), (index_span, sugg_tail.to_string())], | ||
| app, | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
| } | ||
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,56 @@ | ||
| #![feature(new_range_api)] | ||
| #![allow(clippy::redundant_slicing)] | ||
| #![warn(clippy::manual_as_slice)] | ||
|
|
||
| mod nested_1 { | ||
| pub(crate) mod nested_2 { | ||
| pub(crate) const SOME_VALUE: [u8; 4] = [1, 2, 3, 4]; | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| let array: [u8; 4] = [0; 4]; | ||
| let slice: &[u8] = array.as_slice(); | ||
| //~^ manual_as_slice | ||
|
|
||
| let mut array: [u8; 4] = [0; 4]; | ||
| let mut slice: &[u8] = array.as_mut_slice(); | ||
| //~^ manual_as_slice | ||
|
|
||
| let slice: &[u8] = nested_1::nested_2::SOME_VALUE.as_slice(); | ||
| //~^ manual_as_slice | ||
|
|
||
| let slice = b"foo"; | ||
| let slice: &[u8] = slice.as_slice(); | ||
| //~^ manual_as_slice | ||
|
|
||
| let slice = &[1, 2, 3, 4]; | ||
| let slice: &[u8] = slice.as_slice(); | ||
| //~^ manual_as_slice | ||
|
|
||
| macro_rules! perform_the_slice { | ||
| ($a:expr) => { | ||
| $a.as_slice() | ||
| //~^ manual_as_slice | ||
| }; | ||
| } | ||
|
|
||
| perform_the_slice!([1, 2, 3]); | ||
|
|
||
| let slice: &[u8] = vec![1, 2, 3].as_slice(); | ||
| //~^ manual_as_slice | ||
|
|
||
| let slice = &"foo"[..]; | ||
|
|
||
| struct Count; | ||
|
|
||
| impl<R: std::range::RangeBounds<()>> std::ops::Index<R> for Count { | ||
| type Output = (); | ||
|
|
||
| fn index(&self, _: R) -> &Self::Output { | ||
| &() | ||
| } | ||
| } | ||
|
|
||
| let count = &Count[..]; | ||
| } |
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,56 @@ | ||
| #![feature(new_range_api)] | ||
| #![allow(clippy::redundant_slicing)] | ||
| #![warn(clippy::manual_as_slice)] | ||
|
|
||
| mod nested_1 { | ||
| pub(crate) mod nested_2 { | ||
| pub(crate) const SOME_VALUE: [u8; 4] = [1, 2, 3, 4]; | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| let array: [u8; 4] = [0; 4]; | ||
| let slice: &[u8] = &array[..]; | ||
| //~^ manual_as_slice | ||
|
|
||
| let mut array: [u8; 4] = [0; 4]; | ||
| let mut slice: &[u8] = &mut array[..]; | ||
| //~^ manual_as_slice | ||
|
|
||
| let slice: &[u8] = &nested_1::nested_2::SOME_VALUE[..]; | ||
| //~^ manual_as_slice | ||
|
|
||
| let slice = b"foo"; | ||
| let slice: &[u8] = &slice[..]; | ||
| //~^ manual_as_slice | ||
|
|
||
| let slice = &[1, 2, 3, 4]; | ||
| let slice: &[u8] = &slice[..]; | ||
| //~^ manual_as_slice | ||
|
|
||
| macro_rules! perform_the_slice { | ||
| ($a:expr) => { | ||
| &$a[..] | ||
| //~^ manual_as_slice | ||
| }; | ||
| } | ||
|
|
||
| perform_the_slice!([1, 2, 3]); | ||
|
Comment on lines
+31
to
+38
Contributor
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 think we shouldn't lint inside macro definitions, because they may be called with arguments of different types, e.g. the following would be valid: macro_rules! perform_the_slice {
($a:expr) => {
&$a[..]
//~^ manual_as_slice
};
}
perform_the_slice!([1, 2, 3]);
perform_the_slice!(String::new());and putting the |
||
|
|
||
| let slice: &[u8] = &vec![1, 2, 3][..]; | ||
| //~^ manual_as_slice | ||
|
|
||
| let slice = &"foo"[..]; | ||
|
|
||
| struct Count; | ||
|
|
||
| impl<R: std::range::RangeBounds<()>> std::ops::Index<R> for Count { | ||
| type Output = (); | ||
|
|
||
| fn index(&self, _: R) -> &Self::Output { | ||
| &() | ||
| } | ||
| } | ||
|
|
||
| let count = &Count[..]; | ||
| } | ||
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,92 @@ | ||
| error: using a full range slice | ||
| --> tests/ui/manual_as_slice.rs:13:24 | ||
| | | ||
| LL | let slice: &[u8] = &array[..]; | ||
| | ^^^^^^^^^^ | ||
| | | ||
| = note: `-D clippy::manual-as-slice` implied by `-D warnings` | ||
| = help: to override `-D warnings` add `#[allow(clippy::manual_as_slice)]` | ||
| help: try | ||
| | | ||
| LL - let slice: &[u8] = &array[..]; | ||
| LL + let slice: &[u8] = array.as_slice(); | ||
| | | ||
|
|
||
| error: using a full range slice | ||
| --> tests/ui/manual_as_slice.rs:17:28 | ||
| | | ||
| LL | let mut slice: &[u8] = &mut array[..]; | ||
| | ^^^^^^^^^^^^^^ | ||
| | | ||
| help: try | ||
| | | ||
| LL - let mut slice: &[u8] = &mut array[..]; | ||
| LL + let mut slice: &[u8] = array.as_mut_slice(); | ||
| | | ||
|
|
||
| error: using a full range slice | ||
| --> tests/ui/manual_as_slice.rs:20:24 | ||
| | | ||
| LL | let slice: &[u8] = &nested_1::nested_2::SOME_VALUE[..]; | ||
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: try | ||
| | | ||
| LL - let slice: &[u8] = &nested_1::nested_2::SOME_VALUE[..]; | ||
| LL + let slice: &[u8] = nested_1::nested_2::SOME_VALUE.as_slice(); | ||
| | | ||
|
|
||
| error: using a full range slice | ||
| --> tests/ui/manual_as_slice.rs:24:24 | ||
| | | ||
| LL | let slice: &[u8] = &slice[..]; | ||
| | ^^^^^^^^^^ | ||
| | | ||
| help: try | ||
| | | ||
| LL - let slice: &[u8] = &slice[..]; | ||
| LL + let slice: &[u8] = slice.as_slice(); | ||
| | | ||
|
|
||
| error: using a full range slice | ||
| --> tests/ui/manual_as_slice.rs:28:24 | ||
| | | ||
| LL | let slice: &[u8] = &slice[..]; | ||
| | ^^^^^^^^^^ | ||
| | | ||
| help: try | ||
| | | ||
| LL - let slice: &[u8] = &slice[..]; | ||
| LL + let slice: &[u8] = slice.as_slice(); | ||
| | | ||
|
|
||
| error: using a full range slice | ||
| --> tests/ui/manual_as_slice.rs:33:13 | ||
| | | ||
| LL | &$a[..] | ||
| | ^^^^^^^ | ||
| ... | ||
| LL | perform_the_slice!([1, 2, 3]); | ||
| | ----------------------------- in this macro invocation | ||
| | | ||
| = note: this error originates in the macro `perform_the_slice` (in Nightly builds, run with -Z macro-backtrace for more info) | ||
| help: try | ||
| | | ||
| LL - &$a[..] | ||
| LL + $a.as_slice() | ||
| | | ||
|
|
||
| error: using a full range slice | ||
| --> tests/ui/manual_as_slice.rs:40:24 | ||
| | | ||
| LL | let slice: &[u8] = &vec![1, 2, 3][..]; | ||
| | ^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| help: try | ||
| | | ||
| LL - let slice: &[u8] = &vec![1, 2, 3][..]; | ||
| LL + let slice: &[u8] = vec![1, 2, 3].as_slice(); | ||
| | | ||
|
|
||
| error: aborting due to 7 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.
nit: you could construct all of these inside the
|diag| { .. }closure below. That's probably marginally faster, but, more importantly, clearer, as otherwise one could think they're going to be used in some lint logic