-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Add future incompatibility lint for array.into_iter()
#66017
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use crate::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass}; | ||
use rustc::{ | ||
lint::FutureIncompatibleInfo, | ||
hir, | ||
ty::{ | ||
self, | ||
adjustment::{Adjust, Adjustment}, | ||
}, | ||
}; | ||
use syntax::{ | ||
errors::Applicability, | ||
symbol::sym, | ||
}; | ||
|
||
|
||
declare_lint! { | ||
pub ARRAY_INTO_ITER, | ||
Warn, | ||
"detects calling `into_iter` on arrays", | ||
@future_incompatible = FutureIncompatibleInfo { | ||
reference: "issue #66145 <https://github.com/rust-lang/rust/issues/66145>", | ||
edition: None, | ||
}; | ||
} | ||
|
||
declare_lint_pass!( | ||
/// Checks for instances of calling `into_iter` on arrays. | ||
ArrayIntoIter => [ARRAY_INTO_ITER] | ||
); | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ArrayIntoIter { | ||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) { | ||
// We only care about method call expressions. | ||
if let hir::ExprKind::MethodCall(call, span, args) = &expr.kind { | ||
if call.ident.name != sym::into_iter { | ||
return; | ||
} | ||
|
||
// Check if the method call actually calls the libcore | ||
// `IntoIterator::into_iter`. | ||
let def_id = cx.tables.type_dependent_def_id(expr.hir_id).unwrap(); | ||
match cx.tcx.trait_of_item(def_id) { | ||
Some(trait_id) if cx.tcx.is_diagnostic_item(sym::IntoIterator, trait_id) => {}, | ||
_ => return, | ||
}; | ||
|
||
// As this is a method call expression, we have at least one | ||
// argument. | ||
let receiver_arg = &args[0]; | ||
|
||
// Test if the original `self` type is an array type. | ||
match cx.tables.expr_ty(receiver_arg).kind { | ||
ty::Array(..) => {} | ||
_ => return, | ||
} | ||
|
||
// Make sure that the first adjustment is an autoref coercion. | ||
match cx.tables.expr_adjustments(receiver_arg).get(0) { | ||
Some(Adjustment { kind: Adjust::Borrow(_), .. }) => {} | ||
_ => return, | ||
} | ||
|
||
LukasKalbertodt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Emit lint diagnostic. | ||
let target = match cx.tables.expr_ty_adjusted(receiver_arg).kind { | ||
ty::Ref(_, ty::TyS { kind: ty::Array(..), ..}, _) => "[T; N]", | ||
ty::Ref(_, ty::TyS { kind: ty::Slice(..), ..}, _) => "[T]", | ||
|
||
// We know the original first argument type is an array type, | ||
// we know that the first adjustment was an autoref coercion | ||
// and we know that `IntoIterator` is the trait involved. The | ||
// array cannot be coerced to something other than a reference | ||
// to an array or to a slice. | ||
_ => bug!("array type coerced to something other than array or slice"), | ||
}; | ||
let msg = format!( | ||
"this method call currently resolves to `<&{} as IntoIterator>::into_iter` (due \ | ||
to autoref coercions), but that might change in the future when \ | ||
`IntoIterator` impls for arrays are added.", | ||
target, | ||
); | ||
cx.struct_span_lint(ARRAY_INTO_ITER, *span, &msg) | ||
.span_suggestion( | ||
call.ident.span, | ||
"use `.iter()` instead of `.into_iter()` to avoid ambiguity", | ||
"iter".into(), | ||
Applicability::MachineApplicable, | ||
) | ||
.emit(); | ||
} | ||
} | ||
} |
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,33 @@ | ||
// run-pass | ||
// run-rustfix | ||
|
||
fn main() { | ||
let small = [1, 2]; | ||
let big = [0u8; 33]; | ||
|
||
// Expressions that should trigger the lint | ||
small.iter(); | ||
//~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` | ||
//~| WARNING this was previously accepted by the compiler but is being phased out | ||
[1, 2].iter(); | ||
//~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` | ||
//~| WARNING this was previously accepted by the compiler but is being phased out | ||
big.iter(); | ||
//~^ WARNING this method call currently resolves to `<&[T] as IntoIterator>::into_iter` | ||
//~| WARNING this was previously accepted by the compiler but is being phased out | ||
[0u8; 33].iter(); | ||
//~^ WARNING this method call currently resolves to `<&[T] as IntoIterator>::into_iter` | ||
//~| WARNING this was previously accepted by the compiler but is being phased out | ||
|
||
|
||
// Expressions that should not | ||
(&[1, 2]).into_iter(); | ||
(&small).into_iter(); | ||
(&[0u8; 33]).into_iter(); | ||
(&big).into_iter(); | ||
|
||
for _ in &[1, 2] {} | ||
(&small as &[_]).into_iter(); | ||
small[..].into_iter(); | ||
std::iter::IntoIterator::into_iter(&[1, 2]); | ||
} |
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,33 @@ | ||
// run-pass | ||
LukasKalbertodt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// run-rustfix | ||
|
||
fn main() { | ||
let small = [1, 2]; | ||
let big = [0u8; 33]; | ||
|
||
// Expressions that should trigger the lint | ||
small.into_iter(); | ||
LukasKalbertodt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` | ||
//~| WARNING this was previously accepted by the compiler but is being phased out | ||
[1, 2].into_iter(); | ||
//~^ WARNING this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` | ||
//~| WARNING this was previously accepted by the compiler but is being phased out | ||
big.into_iter(); | ||
//~^ WARNING this method call currently resolves to `<&[T] as IntoIterator>::into_iter` | ||
//~| WARNING this was previously accepted by the compiler but is being phased out | ||
[0u8; 33].into_iter(); | ||
//~^ WARNING this method call currently resolves to `<&[T] as IntoIterator>::into_iter` | ||
//~| WARNING this was previously accepted by the compiler but is being phased out | ||
|
||
|
||
// Expressions that should not | ||
(&[1, 2]).into_iter(); | ||
(&small).into_iter(); | ||
(&[0u8; 33]).into_iter(); | ||
(&big).into_iter(); | ||
|
||
for _ in &[1, 2] {} | ||
(&small as &[_]).into_iter(); | ||
small[..].into_iter(); | ||
std::iter::IntoIterator::into_iter(&[1, 2]); | ||
} |
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,37 @@ | ||
warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. | ||
--> $DIR/into-iter-on-arrays-lint.rs:9:11 | ||
| | ||
LL | small.into_iter(); | ||
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` | ||
| | ||
= note: `#[warn(array_into_iter)]` on by default | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #66145 <https://github.com/rust-lang/rust/issues/66145> | ||
|
||
warning: this method call currently resolves to `<&[T; N] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. | ||
--> $DIR/into-iter-on-arrays-lint.rs:12:12 | ||
| | ||
LL | [1, 2].into_iter(); | ||
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #66145 <https://github.com/rust-lang/rust/issues/66145> | ||
|
||
warning: this method call currently resolves to `<&[T] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. | ||
--> $DIR/into-iter-on-arrays-lint.rs:15:9 | ||
| | ||
LL | big.into_iter(); | ||
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #66145 <https://github.com/rust-lang/rust/issues/66145> | ||
|
||
warning: this method call currently resolves to `<&[T] as IntoIterator>::into_iter` (due to autoref coercions), but that might change in the future when `IntoIterator` impls for arrays are added. | ||
--> $DIR/into-iter-on-arrays-lint.rs:18:15 | ||
| | ||
LL | [0u8; 33].into_iter(); | ||
| ^^^^^^^^^ help: use `.iter()` instead of `.into_iter()` to avoid ambiguity: `iter` | ||
| | ||
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! | ||
= note: for more information, see issue #66145 <https://github.com/rust-lang/rust/issues/66145> | ||
|
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: I believe we prefer single line imports.