Edit documentation for for_loop_over_fallibles
lint
This commit is contained in:
parent
75ae20a42f
commit
b9b2059e84
1 changed files with 21 additions and 23 deletions
|
@ -10,43 +10,41 @@ use rustc_span::{sym, Span};
|
||||||
use rustc_trait_selection::traits::TraitEngineExt;
|
use rustc_trait_selection::traits::TraitEngineExt;
|
||||||
|
|
||||||
declare_lint! {
|
declare_lint! {
|
||||||
/// ### What it does
|
|
||||||
///
|
|
||||||
/// Checks for `for` loops over `Option` or `Result` values.
|
/// Checks for `for` loops over `Option` or `Result` values.
|
||||||
///
|
///
|
||||||
/// ### Why is this bad?
|
/// ### Explanation
|
||||||
/// Readability. This is more clearly expressed as an `if
|
///
|
||||||
/// let`.
|
/// Both `Option` and `Result` implement `IntoIterator` trait, which allows using them in a `for` loop.
|
||||||
|
/// `for` loop over `Option` or `Result` will iterate either 0 (if the value is `None`/`Err(_)`)
|
||||||
|
/// or 1 time (if the value is `Some(_)`/`Ok(_)`). This is not very useful and is more clearly expressed
|
||||||
|
/// via `if let`.
|
||||||
|
///
|
||||||
|
/// `for` loop can also be accidentally written with the intention to call a function multiple times,
|
||||||
|
/// while the function returns `Some(_)`, in these cases `while let` loop should be used instead.
|
||||||
|
///
|
||||||
|
/// The "intended" use of `IntoIterator` implementations for `Option` and `Result` is passing them to
|
||||||
|
/// generic code that expects something implementing `IntoIterator`. For example using `.chain(option)`
|
||||||
|
/// to optionally add a value to an iterator.
|
||||||
///
|
///
|
||||||
/// ### Example
|
/// ### Example
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # let opt = Some(1);
|
/// # let opt = Some(1);
|
||||||
/// # let res: Result<i32, std::io::Error> = Ok(1);
|
/// # let res: Result<i32, std::io::Error> = Ok(1);
|
||||||
/// for x in opt {
|
/// # let recv = || Some(1);
|
||||||
/// // ..
|
/// for x in opt { /* ... */}
|
||||||
/// }
|
/// for x in res { /* ... */ }
|
||||||
///
|
/// for x in recv() { /* ... */ }
|
||||||
/// for x in &res {
|
|
||||||
/// // ..
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// for x in res.iter() {
|
|
||||||
/// // ..
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Use instead:
|
/// Use instead:
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # let opt = Some(1);
|
/// # let opt = Some(1);
|
||||||
/// # let res: Result<i32, std::io::Error> = Ok(1);
|
/// # let res: Result<i32, std::io::Error> = Ok(1);
|
||||||
/// if let Some(x) = opt {
|
/// # let recv = || Some(1);
|
||||||
/// // ..
|
/// if let Some(x) = opt { /* ... */}
|
||||||
/// }
|
/// if let Ok(x) = res { /* ... */ }
|
||||||
///
|
/// while let Some(x) = recv() { /* ... */ }
|
||||||
/// if let Ok(x) = res {
|
|
||||||
/// // ..
|
|
||||||
/// }
|
|
||||||
/// ```
|
/// ```
|
||||||
pub FOR_LOOP_OVER_FALLIBLES,
|
pub FOR_LOOP_OVER_FALLIBLES,
|
||||||
Warn,
|
Warn,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue