|
| 1 | +use clippy_utils::{diagnostics::span_lint_and_then, is_trait_method, match_def_path, match_trait_method, paths}; |
| 2 | +use rustc_errors::Applicability; |
| 3 | +use rustc_hir::{Expr, ExprKind}; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; |
| 6 | +use rustc_span::sym; |
| 7 | + |
| 8 | +declare_clippy_lint! { |
| 9 | + /// ### What it does |
| 10 | + /// Detect uses of `b.lines().filter_map(Result::ok)` or `b.lines().flat_map(Result::ok)` |
| 11 | + /// when `b` implements `BufRead`. |
| 12 | + /// |
| 13 | + /// ### Why is this bad? |
| 14 | + /// `b.lines()` might produce an infinite stream of `Err` and `filter_map(Result::ok)` |
| 15 | + /// will also run forever. Using `map_while(Result::ok)` would stop after the first |
| 16 | + /// `Err` is emitted. |
| 17 | + /// |
| 18 | + /// For example, on some platforms, `std::fs::File::open(path)` might return `Ok(fs)` |
| 19 | + /// when `path` is a directory, and reading from `fs` will then return an error. |
| 20 | + /// If `fs` was inadvertently put in a `BufReader`, calling `lines()` on it would |
| 21 | + /// return an infinite stream of `Err` variants. |
| 22 | + /// |
| 23 | + /// ### Example |
| 24 | + /// ```rust |
| 25 | + /// #use std::{fs::File, io::{BufRead, BufReader}}; |
| 26 | + /// let reader = BufReader::new(File::open("some path")?); |
| 27 | + /// for line in reader.lines().flat_map(Result::ok) { |
| 28 | + /// println!("{line}"); |
| 29 | + /// } |
| 30 | + /// ``` |
| 31 | + /// Use instead: |
| 32 | + /// ```rust |
| 33 | + /// #use std::{fs::File, io::{BufRead, BufReader}}; |
| 34 | + /// let reader = BufReader::new(File::open("some path")?); |
| 35 | + /// for line in reader.lines().map_while(Result::ok) { |
| 36 | + /// println!("{line}"); |
| 37 | + /// } |
| 38 | + /// ``` |
| 39 | + #[clippy::version = "1.70.0"] |
| 40 | + pub LINES_FILTER_MAP_OK, |
| 41 | + nursery, |
| 42 | + "`BufRead::lines()` might produce an infinite stream of errors" |
| 43 | +} |
| 44 | +declare_lint_pass!(LinesFilterMapOk => [LINES_FILTER_MAP_OK]); |
| 45 | + |
| 46 | +impl LateLintPass<'_> for LinesFilterMapOk { |
| 47 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 48 | + if let ExprKind::MethodCall(fm_method, fm_receiver, [fm_arg], fm_span) = expr.kind && |
| 49 | + is_trait_method(cx, expr, sym::Iterator) && |
| 50 | + (fm_method.ident.as_str() == "filter_map" || fm_method.ident.as_str() == "flat_map") && |
| 51 | + let ExprKind::MethodCall(lines_method, _, &[], _) = fm_receiver.kind && |
| 52 | + match_trait_method(cx, fm_receiver, &paths::STD_IO_BUFREAD) && |
| 53 | + lines_method.ident.as_str() == "lines" && |
| 54 | + let ExprKind::Path(qpath) = &fm_arg.kind && |
| 55 | + let Some(did) = cx.qpath_res(qpath, fm_arg.hir_id).opt_def_id() && |
| 56 | + match_def_path(cx, did, &paths::CORE_RESULT_OK_METHOD) |
| 57 | + { |
| 58 | + span_lint_and_then(cx, |
| 59 | + LINES_FILTER_MAP_OK, |
| 60 | + fm_span, |
| 61 | + &format!("`{}()` will run forever on an infinite stream of `Err` returned by `lines()`", fm_method.ident), |
| 62 | + |diag| { |
| 63 | + diag.span_note( |
| 64 | + fm_receiver.span, |
| 65 | + "`BufRead::lines()` may produce an infinite stream of `Err` in case of a read error"); |
| 66 | + diag.span_suggestion(fm_span, "replace with", "map_while(Result::ok)", Applicability::MaybeIncorrect); |
| 67 | + }); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments