-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add new lint suspicious_slice_copies
#16671
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
faeroon
wants to merge
4
commits into
rust-lang:master
Choose a base branch
from
faeroon:issues/16507
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.
+466
−0
Open
Changes from all commits
Commits
Show all changes
4 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,110 @@ | ||
| use super::SUSPICIOUS_SLICE_COPIES; | ||
| use clippy_utils::diagnostics::span_lint_and_then; | ||
| use clippy_utils::msrvs::Msrv; | ||
| use clippy_utils::source::{IntoSpan, SpanRangeExt, snippet_with_context}; | ||
| use clippy_utils::{msrvs, sym}; | ||
| use rustc_ast::{BorrowKind, Mutability}; | ||
| use rustc_errors::Applicability; | ||
| use rustc_hir::{Expr, ExprKind, Node}; | ||
| use rustc_lint::LateContext; | ||
| use rustc_middle::ty; | ||
| use rustc_middle::ty::{GenericArgKind, Ty}; | ||
| use rustc_span::Span; | ||
|
|
||
| enum TryIntoReceiver { | ||
| Slice, | ||
| SliceMutRef, | ||
| } | ||
|
|
||
| impl TryIntoReceiver { | ||
| fn from_ty(ty: Ty<'_>) -> Option<Self> { | ||
| match ty.kind() { | ||
| ty::Slice(_) => Some(TryIntoReceiver::Slice), | ||
| ty::Ref(_, ref_ty, Mutability::Mut) if let ty::Slice(_) = ref_ty.kind() => { | ||
| Some(TryIntoReceiver::SliceMutRef) | ||
| }, | ||
| _ => None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(super) fn check( | ||
| cx: &LateContext<'_>, | ||
| try_into_expr: &Expr<'_>, | ||
| recv_expr: &Expr<'_>, | ||
| try_into_span: Span, | ||
| msrv: Msrv, | ||
| ) { | ||
| if let Some(receiver) = TryIntoReceiver::from_ty(cx.typeck_results().expr_ty(recv_expr)) | ||
|
|
||
| // try_into was instantiated with [T; N] | ||
| && let [_, dst_ty] = &**cx.typeck_results().node_args(try_into_expr.hir_id) | ||
| && let GenericArgKind::Type(arg_ty) = dst_ty.kind() | ||
| && let ty::Array(..) = arg_ty.kind() | ||
|
|
||
| && let mut parent_iter = cx.tcx.hir_parent_iter(try_into_expr.hir_id) | ||
|
|
||
| // calls unwrap() or expect() | ||
| && let Some((_, Node::Expr(unwrap_expr))) = parent_iter.next() | ||
| && let ExprKind::MethodCall(path, .., unwrap_span) = unwrap_expr.kind | ||
| && let sym::unwrap | sym::expect = path.ident.name | ||
|
|
||
| // use mut ref | ||
| && let Some((_, Node::Expr(mut_ref_expr))) = parent_iter.next() | ||
| && let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, inner_expr) = mut_ref_expr.kind | ||
| { | ||
| let span = mut_ref_expr.span.with_hi(inner_expr.span.hi()); | ||
|
|
||
| span_lint_and_then( | ||
| cx, | ||
| SUSPICIOUS_SLICE_COPIES, | ||
| span, | ||
| "using a mutable reference to temporary array", | ||
| |diag| { | ||
| let mut applicability = Applicability::MaybeIncorrect; | ||
|
|
||
| let (recv_snippet, _) = | ||
| snippet_with_context(cx, recv_expr.span, try_into_span.ctxt(), "..", &mut applicability); | ||
| let (unwrap_snippet, _) = | ||
| snippet_with_context(cx, unwrap_span, try_into_span.ctxt(), "..", &mut applicability); | ||
| let (mut_ref_snippet, _) = snippet_with_context( | ||
| cx, | ||
| mut_ref_expr | ||
| .span | ||
| .until(inner_expr.span.with_leading_whitespace(cx).into_span()), | ||
| try_into_span.ctxt(), | ||
| "..", | ||
| &mut applicability, | ||
| ); | ||
|
|
||
| let borrow_sugg = if msrv.meets(cx, msrvs::SLICE_AS_MUT_ARRAY) { | ||
| format!("{recv_snippet}.as_mut_array().{unwrap_snippet}") | ||
| } else { | ||
| match receiver { | ||
| TryIntoReceiver::Slice => { | ||
| format!("({mut_ref_snippet} {recv_snippet}).try_into().{unwrap_snippet}") | ||
| }, | ||
| TryIntoReceiver::SliceMutRef => format!("{recv_snippet}.try_into().{unwrap_snippet}"), | ||
| } | ||
| }; | ||
|
|
||
| let copy_sugg = match receiver { | ||
| TryIntoReceiver::Slice => { | ||
| format!("{mut_ref_snippet} <[_; _]>::try_from(&{recv_snippet}).{unwrap_snippet}") | ||
| }, | ||
| TryIntoReceiver::SliceMutRef => { | ||
| format!("{mut_ref_snippet} <[_; _]>::try_from({recv_snippet}).{unwrap_snippet}") | ||
| }, | ||
| }; | ||
|
|
||
| diag.span_suggestion(span, "borrow the slice as an array", borrow_sugg, applicability); | ||
| diag.span_suggestion( | ||
| span, | ||
| "explicitly create a new array using `TryFrom`", | ||
| copy_sugg, | ||
| applicability, | ||
| ); | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
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,58 @@ | ||
| #![warn(clippy::suspicious_slice_copies)] | ||
|
|
||
| const K: usize = 3; | ||
| const N: usize = 5; | ||
|
|
||
| fn arr_incr(arr: &mut [i32; K]) { | ||
| for x in arr.iter_mut() { | ||
| *x += 1; | ||
| } | ||
| } | ||
|
|
||
| #[clippy::msrv = "1.92.0"] | ||
| fn test_msrv_1_92() { | ||
| let mut arr: [i32; N] = [0; 5]; | ||
| let slice: &mut [i32] = &mut arr[..K]; | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = slice.try_into().unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = (&mut arr[..K]).try_into().unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = (&mut arr[..K]).try_into().unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| arr_incr((&mut arr[..K]).try_into().expect("never happen")); | ||
| //~^ suspicious_slice_copies | ||
| } | ||
|
|
||
| #[clippy::msrv = "1.93.0"] | ||
| fn test_msrv_1_93() { | ||
| let mut arr: [i32; N] = [0; 5]; | ||
| let slice: &mut [i32] = &mut arr[..K]; | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = slice.as_mut_array().unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = arr[..K].as_mut_array().unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = arr[..K].as_mut_array().unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| arr_incr(arr[..K].as_mut_array().expect("never happen")); | ||
| //~^ suspicious_slice_copies | ||
| } | ||
|
|
||
| fn main() { | ||
| let mut arr: [i32; N] = [0; 5]; | ||
|
|
||
| let arr_ref: &[i32; K] = &arr[..K].try_into().unwrap(); | ||
| let arr_mut_ref: &mut [i32; K] = (&mut arr[..K]).try_into().unwrap(); | ||
|
|
||
| let arr_copy: [i32; K] = arr[..K].try_into().unwrap(); | ||
|
|
||
| let slice: &[i32] = &arr[..K]; | ||
| let arr_mut_ref: &mut [i32; K] = &mut slice.try_into().unwrap(); | ||
| } |
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,58 @@ | ||
| #![warn(clippy::suspicious_slice_copies)] | ||
|
|
||
| const K: usize = 3; | ||
| const N: usize = 5; | ||
|
|
||
| fn arr_incr(arr: &mut [i32; K]) { | ||
| for x in arr.iter_mut() { | ||
| *x += 1; | ||
| } | ||
| } | ||
|
|
||
| #[clippy::msrv = "1.92.0"] | ||
| fn test_msrv_1_92() { | ||
| let mut arr: [i32; N] = [0; 5]; | ||
| let slice: &mut [i32] = &mut arr[..K]; | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut <[_; _]>::try_from(slice).unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut <[_; _]>::try_from(&arr[..K]).unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut <[_; _]>::try_from(&arr[..K]).unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| arr_incr(&mut <[_; _]>::try_from(&arr[..K]).expect("never happen")); | ||
| //~^ suspicious_slice_copies | ||
| } | ||
|
|
||
| #[clippy::msrv = "1.93.0"] | ||
| fn test_msrv_1_93() { | ||
| let mut arr: [i32; N] = [0; 5]; | ||
| let slice: &mut [i32] = &mut arr[..K]; | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut <[_; _]>::try_from(slice).unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut <[_; _]>::try_from(&arr[..K]).unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut <[_; _]>::try_from(&arr[..K]).unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| arr_incr(&mut <[_; _]>::try_from(&arr[..K]).expect("never happen")); | ||
| //~^ suspicious_slice_copies | ||
| } | ||
|
|
||
| fn main() { | ||
| let mut arr: [i32; N] = [0; 5]; | ||
|
|
||
| let arr_ref: &[i32; K] = &arr[..K].try_into().unwrap(); | ||
| let arr_mut_ref: &mut [i32; K] = (&mut arr[..K]).try_into().unwrap(); | ||
|
|
||
| let arr_copy: [i32; K] = arr[..K].try_into().unwrap(); | ||
|
|
||
| let slice: &[i32] = &arr[..K]; | ||
| let arr_mut_ref: &mut [i32; K] = &mut slice.try_into().unwrap(); | ||
| } |
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,58 @@ | ||
| #![warn(clippy::suspicious_slice_copies)] | ||
|
|
||
| const K: usize = 3; | ||
| const N: usize = 5; | ||
|
|
||
| fn arr_incr(arr: &mut [i32; K]) { | ||
| for x in arr.iter_mut() { | ||
| *x += 1; | ||
| } | ||
| } | ||
|
|
||
| #[clippy::msrv = "1.92.0"] | ||
| fn test_msrv_1_92() { | ||
| let mut arr: [i32; N] = [0; 5]; | ||
| let slice: &mut [i32] = &mut arr[..K]; | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut slice.try_into().unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut arr[..K].try_into().unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut (arr[..K].try_into().unwrap()); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| arr_incr(&mut arr[..K].try_into().expect("never happen")); | ||
| //~^ suspicious_slice_copies | ||
| } | ||
|
|
||
| #[clippy::msrv = "1.93.0"] | ||
| fn test_msrv_1_93() { | ||
| let mut arr: [i32; N] = [0; 5]; | ||
| let slice: &mut [i32] = &mut arr[..K]; | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut slice.try_into().unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut arr[..K].try_into().unwrap(); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| let mut_arr_ref: &mut [i32; K] = &mut (arr[..K].try_into().unwrap()); | ||
| //~^ suspicious_slice_copies | ||
|
|
||
| arr_incr(&mut arr[..K].try_into().expect("never happen")); | ||
| //~^ suspicious_slice_copies | ||
| } | ||
|
|
||
| fn main() { | ||
| let mut arr: [i32; N] = [0; 5]; | ||
|
|
||
| let arr_ref: &[i32; K] = &arr[..K].try_into().unwrap(); | ||
| let arr_mut_ref: &mut [i32; K] = (&mut arr[..K]).try_into().unwrap(); | ||
|
|
||
| let arr_copy: [i32; K] = arr[..K].try_into().unwrap(); | ||
|
|
||
| let slice: &[i32] = &arr[..K]; | ||
| let arr_mut_ref: &mut [i32; K] = &mut slice.try_into().unwrap(); | ||
| } |
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.
nit: This should also use the type from the
node_argscall to save the extra lookup.This isn't a correctness issue in this case due to the impls involved, but it would be for most other traits since the receiver's type could be adjusted.
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.
As far as I understood, if we use type from
node_args, we can't distinguish between&[T]and[T]and it could lead to false-positive lint emitting for&[T]:because we can't convert from
&[T]to&mut [T; K].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.
To illustrate that, here is debug info.
For
&[T]:For
&mut [T]:For
[T]: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.
This actually points out an issue with the current logic. Right now this is linting:
If the receiver type is just
[T]there's no guarantee it can be mutably borrowed. You'll need a more involved check to see if that slice can be borrowed mutably. I know we do this somewhere and I'll see if I can find it.