-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
213 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use clippy_config::Conf; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::msrvs::{IO_ERROR_OTHER, Msrv}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::impl_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// This lint warns on calling `io::Error::new(..)` with a kind of | ||
/// `io::ErrorKind::Other`. | ||
/// | ||
/// ### Why is this bad? | ||
/// Since Rust 1.74, there's the `io::Error::other(_)` shortcut. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// use std::io; | ||
/// let _ = io::Error::new(io::ErrorKind::Other, "bad".to_string()); | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// let _ = io::Error::other("bad".to_string()); | ||
/// ``` | ||
#[clippy::version = "1.86.0"] | ||
pub IO_OTHER_ERROR, | ||
complexity, | ||
"calling `io::Error::new(io::ErrorKind::Other, _)`" | ||
} | ||
|
||
pub struct IoOtherError { | ||
msrv: Msrv, | ||
} | ||
|
||
impl_lint_pass!(IoOtherError => [IO_OTHER_ERROR]); | ||
|
||
impl IoOtherError { | ||
pub fn new(conf: &'static Conf) -> Self { | ||
Self { | ||
msrv: conf.msrv.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl LateLintPass<'_> for IoOtherError { | ||
extract_msrv_attr!(LateContext); | ||
|
||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { | ||
if let ExprKind::Call(path, [error_kind, error]) = expr.kind | ||
&& !expr.span.from_expansion() | ||
&& !error_kind.span.from_expansion() | ||
&& clippy_utils::is_expr_path_def_path(cx, path, &clippy_utils::paths::IO_ERROR_NEW) | ||
&& clippy_utils::is_expr_path_def_path( | ||
cx, | ||
clippy_utils::expr_or_init(cx, error_kind), | ||
&clippy_utils::paths::IO_ERRORKIND_OTHER, | ||
) | ||
&& self.msrv.meets(IO_ERROR_OTHER) | ||
{ | ||
let mut applicability = Applicability::MachineApplicable; | ||
let snip = clippy_utils::source::snippet_with_applicability(cx, error.span, "_", &mut applicability); | ||
span_lint_and_sugg( | ||
cx, | ||
IO_OTHER_ERROR, | ||
expr.span, | ||
"this can be `io::Error::other`", | ||
"use", | ||
format!("std::io::Error::other({snip})"), | ||
applicability, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.