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