lint: port array-into-iter diagnostics
Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
parent
7d2eba6311
commit
0f4c4c5e18
3 changed files with 19 additions and 13 deletions
6
compiler/rustc_error_messages/locales/en-US/lint.ftl
Normal file
6
compiler/rustc_error_messages/locales/en-US/lint.ftl
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
lint-array-into-iter =
|
||||||
|
this method call resolves to `<&{$target} as IntoIterator>::into_iter` (due to backwards compatibility), but will resolve to <{$target} as IntoIterator>::into_iter in Rust 2021
|
||||||
|
.use-iter-suggestion = use `.iter()` instead of `.into_iter()` to avoid ambiguity
|
||||||
|
.remove-into-iter-suggestion = or remove `.into_iter()` to iterate by value
|
||||||
|
.use-explicit-into-iter-suggestion =
|
||||||
|
or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value
|
|
@ -31,11 +31,12 @@ pub use unic_langid::{langid, LanguageIdentifier};
|
||||||
|
|
||||||
// Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.
|
// Generates `DEFAULT_LOCALE_RESOURCES` static and `fluent_generated` module.
|
||||||
fluent_messages! {
|
fluent_messages! {
|
||||||
|
borrowck => "../locales/en-US/borrowck.ftl",
|
||||||
|
builtin_macros => "../locales/en-US/builtin_macros.ftl",
|
||||||
|
lint => "../locales/en-US/lint.ftl",
|
||||||
parser => "../locales/en-US/parser.ftl",
|
parser => "../locales/en-US/parser.ftl",
|
||||||
privacy => "../locales/en-US/privacy.ftl",
|
privacy => "../locales/en-US/privacy.ftl",
|
||||||
typeck => "../locales/en-US/typeck.ftl",
|
typeck => "../locales/en-US/typeck.ftl",
|
||||||
builtin_macros => "../locales/en-US/builtin_macros.ftl",
|
|
||||||
borrowck => "../locales/en-US/borrowck.ftl",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};
|
pub use fluent_generated::{self as fluent, DEFAULT_LOCALE_RESOURCES};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{LateContext, LateLintPass, LintContext};
|
use crate::{LateContext, LateLintPass, LintContext};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::{fluent, Applicability};
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
use rustc_middle::ty::adjustment::{Adjust, Adjustment};
|
use rustc_middle::ty::adjustment::{Adjust, Adjustment};
|
||||||
|
@ -120,31 +120,30 @@ impl<'tcx> LateLintPass<'tcx> for ArrayIntoIter {
|
||||||
_ => bug!("array type coerced to something other than array or slice"),
|
_ => bug!("array type coerced to something other than array or slice"),
|
||||||
};
|
};
|
||||||
cx.struct_span_lint(ARRAY_INTO_ITER, call.ident.span, |lint| {
|
cx.struct_span_lint(ARRAY_INTO_ITER, call.ident.span, |lint| {
|
||||||
let mut diag = lint.build(&format!(
|
let mut diag = lint.build(fluent::lint::array_into_iter);
|
||||||
"this method call resolves to `<&{} as IntoIterator>::into_iter` \
|
diag.set_arg("target", target);
|
||||||
(due to backwards compatibility), \
|
|
||||||
but will resolve to <{} as IntoIterator>::into_iter in Rust 2021",
|
|
||||||
target, target,
|
|
||||||
));
|
|
||||||
diag.span_suggestion(
|
diag.span_suggestion(
|
||||||
call.ident.span,
|
call.ident.span,
|
||||||
"use `.iter()` instead of `.into_iter()` to avoid ambiguity",
|
fluent::lint::use_iter_suggestion,
|
||||||
"iter",
|
"iter",
|
||||||
Applicability::MachineApplicable,
|
Applicability::MachineApplicable,
|
||||||
);
|
);
|
||||||
if self.for_expr_span == expr.span {
|
if self.for_expr_span == expr.span {
|
||||||
diag.span_suggestion(
|
diag.span_suggestion(
|
||||||
receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
|
receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
|
||||||
"or remove `.into_iter()` to iterate by value",
|
fluent::lint::remove_into_iter_suggestion,
|
||||||
"",
|
"",
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
} else if receiver_ty.is_array() {
|
} else if receiver_ty.is_array() {
|
||||||
diag.multipart_suggestion(
|
diag.multipart_suggestion(
|
||||||
"or use `IntoIterator::into_iter(..)` instead of `.into_iter()` to explicitly iterate by value",
|
fluent::lint::use_explicit_into_iter_suggestion,
|
||||||
vec![
|
vec![
|
||||||
(expr.span.shrink_to_lo(), "IntoIterator::into_iter(".into()),
|
(expr.span.shrink_to_lo(), "IntoIterator::into_iter(".into()),
|
||||||
(receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()), ")".into()),
|
(
|
||||||
|
receiver_arg.span.shrink_to_hi().to(expr.span.shrink_to_hi()),
|
||||||
|
")".into(),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
Applicability::MaybeIncorrect,
|
Applicability::MaybeIncorrect,
|
||||||
);
|
);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue