1
Fork 0

Add future_prelude_collision lint

This commit is contained in:
jam1garner 2021-05-25 23:27:26 -04:00 committed by Niko Matsakis
parent a216131c35
commit 79388aa067
3 changed files with 95 additions and 1 deletions

View file

@ -3001,6 +3001,7 @@ declare_lint_pass! {
PROC_MACRO_BACK_COMPAT,
OR_PATTERNS_BACK_COMPAT,
LARGE_ASSIGNMENTS,
FUTURE_PRELUDE_COLLISION,
]
}
@ -3240,3 +3241,47 @@ declare_lint! {
Allow,
"detects usage of old versions of or-patterns",
}
declare_lint! {
/// The `future_prelude_collision` lint detects the usage of trait methods which are ambiguous
/// with traits added to the prelude in future editions.
///
/// ### Example
///
/// ```rust,compile_fail
/// #![deny(future_prelude_collision)]
///
/// trait Foo {
/// fn try_into(self) -> Result<String, !>;
/// }
///
/// impl Foo for &str {
/// fn try_into(self) -> Result<String, !> {
/// Ok(String::from(self))
/// }
/// }
///
/// fn main() {
/// let x: String = "3".try_into().unwrap();
/// // ^^^^^^^^
/// // This call to try_into matches both Foo:try_into and TryInto::try_into as
/// // `TryInto` has been added to the Rust prelude in 2021 edition.
/// println!("{}", x);
/// }
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// In Rust 2021, one of the important introductions is the [prelude changes], which add
/// `TryFrom` and `TryInto` into the standard library's prelude. Since this results in an
/// amiguity as to which method to call when an existing `try_from` or `try_into` method is
/// called via dot-call syntax.
///
/// [prelude changes]: https://blog.rust-lang.org/inside-rust/2021/03/04/planning-rust-2021.html#prelude-changes
pub FUTURE_PRELUDE_COLLISION,
Warn,
"detects the usage of trait methods which are ambiguous with traits added to the \
prelude in future editions",
}