mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-10 23:24:24 +00:00
PR comments
This commit is contained in:
parent
60a80849ce
commit
1e6c6976dd
4 changed files with 19 additions and 23 deletions
|
@ -1,4 +1,4 @@
|
||||||
use crate::utils::{match_qpath, paths, snippet, span_lint_and_then};
|
use crate::utils::{match_qpath, paths, snippet, span_lint_and_sugg};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc::hir::*;
|
use rustc::hir::*;
|
||||||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
||||||
|
@ -17,17 +17,17 @@ declare_clippy_lint! {
|
||||||
/// **Known problems:** None.
|
/// **Known problems:** None.
|
||||||
///
|
///
|
||||||
/// **Example:**
|
/// **Example:**
|
||||||
///
|
/// ```rust
|
||||||
/// ```rust,ignore
|
|
||||||
/// // Bad
|
|
||||||
/// fn foo(fail: bool) -> Result<i32, String> {
|
/// fn foo(fail: bool) -> Result<i32, String> {
|
||||||
/// if fail {
|
/// if fail {
|
||||||
/// Err("failed")?;
|
/// Err("failed")?;
|
||||||
/// }
|
/// }
|
||||||
/// Ok(0)
|
/// Ok(0)
|
||||||
/// }
|
/// }
|
||||||
|
/// ```
|
||||||
|
/// Could be written:
|
||||||
///
|
///
|
||||||
/// // Good
|
/// ```rust
|
||||||
/// fn foo(fail: bool) -> Result<i32, String> {
|
/// fn foo(fail: bool) -> Result<i32, String> {
|
||||||
/// if fail {
|
/// if fail {
|
||||||
/// return Err("failed".into());
|
/// return Err("failed".into());
|
||||||
|
@ -57,7 +57,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
|
||||||
if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.node;
|
if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.node;
|
||||||
if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.node;
|
if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.node;
|
||||||
if let ExprKind::Path(ref match_fun_path) = match_fun.node;
|
if let ExprKind::Path(ref match_fun_path) = match_fun.node;
|
||||||
if match_qpath(match_fun_path, &["std", "ops", "Try", "into_result"]);
|
if match_qpath(match_fun_path, &paths::TRY_INTO_RESULT);
|
||||||
if let Some(ref try_arg) = try_args.get(0);
|
if let Some(ref try_arg) = try_args.get(0);
|
||||||
if let ExprKind::Call(ref err_fun, ref err_args) = try_arg.node;
|
if let ExprKind::Call(ref err_fun, ref err_args) = try_arg.node;
|
||||||
if let Some(ref err_arg) = err_args.get(0);
|
if let Some(ref err_arg) = err_args.get(0);
|
||||||
|
@ -73,20 +73,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
|
||||||
format!("return Err({}.into())", snippet(cx, err_arg.span, "_"))
|
format!("return Err({}.into())", snippet(cx, err_arg.span, "_"))
|
||||||
};
|
};
|
||||||
|
|
||||||
span_lint_and_then(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
TRY_ERR,
|
TRY_ERR,
|
||||||
expr.span,
|
expr.span,
|
||||||
&format!("confusing error return, consider using `{}`", suggestion),
|
"returning an `Err(_)` with the `?` operator",
|
||||||
|db| {
|
|
||||||
db.span_suggestion(
|
|
||||||
expr.span,
|
|
||||||
"try this",
|
"try this",
|
||||||
suggestion,
|
suggestion,
|
||||||
Applicability::MaybeIncorrect
|
Applicability::MaybeIncorrect
|
||||||
);
|
);
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,7 +92,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
|
||||||
// its output type.
|
// its output type.
|
||||||
fn find_err_return_type<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx ExprKind) -> Option<Ty<'tcx>> {
|
fn find_err_return_type<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx ExprKind) -> Option<Ty<'tcx>> {
|
||||||
if let ExprKind::Match(_, ref arms, MatchSource::TryDesugar) = expr {
|
if let ExprKind::Match(_, ref arms, MatchSource::TryDesugar) = expr {
|
||||||
arms.iter().filter_map(|ty| find_err_return_type_arm(cx, ty)).nth(0)
|
arms.iter().find_map(|ty| find_err_return_type_arm(cx, ty))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -109,7 +104,7 @@ fn find_err_return_type_arm<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, arm: &'tcx Arm
|
||||||
if let ExprKind::Ret(Some(ref err_ret)) = arm.body.node;
|
if let ExprKind::Ret(Some(ref err_ret)) = arm.body.node;
|
||||||
if let ExprKind::Call(ref from_error_path, ref from_error_args) = err_ret.node;
|
if let ExprKind::Call(ref from_error_path, ref from_error_args) = err_ret.node;
|
||||||
if let ExprKind::Path(ref from_error_fn) = from_error_path.node;
|
if let ExprKind::Path(ref from_error_fn) = from_error_path.node;
|
||||||
if match_qpath(from_error_fn, &["std", "ops", "Try", "from_error"]);
|
if match_qpath(from_error_fn, &paths::TRY_FROM_ERROR);
|
||||||
if let Some(from_error_arg) = from_error_args.get(0);
|
if let Some(from_error_arg) = from_error_args.get(0);
|
||||||
then {
|
then {
|
||||||
Some(cx.tables.expr_ty(from_error_arg))
|
Some(cx.tables.expr_ty(from_error_arg))
|
||||||
|
|
|
@ -107,6 +107,7 @@ pub const TO_OWNED_METHOD: [&str; 4] = ["alloc", "borrow", "ToOwned", "to_owned"
|
||||||
pub const TO_STRING: [&str; 3] = ["alloc", "string", "ToString"];
|
pub const TO_STRING: [&str; 3] = ["alloc", "string", "ToString"];
|
||||||
pub const TO_STRING_METHOD: [&str; 4] = ["alloc", "string", "ToString", "to_string"];
|
pub const TO_STRING_METHOD: [&str; 4] = ["alloc", "string", "ToString", "to_string"];
|
||||||
pub const TRANSMUTE: [&str; 4] = ["core", "intrinsics", "", "transmute"];
|
pub const TRANSMUTE: [&str; 4] = ["core", "intrinsics", "", "transmute"];
|
||||||
|
pub const TRY_FROM_ERROR: [&str; 4] = ["std", "ops", "Try", "from_error"];
|
||||||
pub const TRY_INTO_RESULT: [&str; 4] = ["std", "ops", "Try", "into_result"];
|
pub const TRY_INTO_RESULT: [&str; 4] = ["std", "ops", "Try", "into_result"];
|
||||||
pub const UNINIT: [&str; 4] = ["core", "intrinsics", "", "uninit"];
|
pub const UNINIT: [&str; 4] = ["core", "intrinsics", "", "uninit"];
|
||||||
pub const VEC: [&str; 3] = ["alloc", "vec", "Vec"];
|
pub const VEC: [&str; 3] = ["alloc", "vec", "Vec"];
|
||||||
|
|
|
@ -1823,7 +1823,7 @@ pub const ALL_LINTS: [Lint; 306] = [
|
||||||
Lint {
|
Lint {
|
||||||
name: "try_err",
|
name: "try_err",
|
||||||
group: "style",
|
group: "style",
|
||||||
desc: "TODO",
|
desc: "return errors explicitly rather than hiding them behind a `?`",
|
||||||
deprecation: None,
|
deprecation: None,
|
||||||
module: "try_err",
|
module: "try_err",
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
error: confusing error return, consider using `return Err(err)`
|
error: returning an `Err(_)` with the `?` operator
|
||||||
--> $DIR/try_err.rs:7:5
|
--> $DIR/try_err.rs:7:5
|
||||||
|
|
|
|
||||||
LL | Err(err)?;
|
LL | Err(err)?;
|
||||||
|
@ -10,19 +10,19 @@ note: lint level defined here
|
||||||
LL | #![deny(clippy::try_err)]
|
LL | #![deny(clippy::try_err)]
|
||||||
| ^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
error: confusing error return, consider using `return Err(err.into())`
|
error: returning an `Err(_)` with the `?` operator
|
||||||
--> $DIR/try_err.rs:14:5
|
--> $DIR/try_err.rs:14:5
|
||||||
|
|
|
|
||||||
LL | Err(err)?;
|
LL | Err(err)?;
|
||||||
| ^^^^^^^^^ help: try this: `return Err(err.into())`
|
| ^^^^^^^^^ help: try this: `return Err(err.into())`
|
||||||
|
|
||||||
error: confusing error return, consider using `return Err(err)`
|
error: returning an `Err(_)` with the `?` operator
|
||||||
--> $DIR/try_err.rs:31:13
|
--> $DIR/try_err.rs:31:13
|
||||||
|
|
|
|
||||||
LL | Err(err)?;
|
LL | Err(err)?;
|
||||||
| ^^^^^^^^^ help: try this: `return Err(err)`
|
| ^^^^^^^^^ help: try this: `return Err(err)`
|
||||||
|
|
||||||
error: confusing error return, consider using `return Err(err.into())`
|
error: returning an `Err(_)` with the `?` operator
|
||||||
--> $DIR/try_err.rs:46:13
|
--> $DIR/try_err.rs:46:13
|
||||||
|
|
|
|
||||||
LL | Err(err)?;
|
LL | Err(err)?;
|
||||||
|
|
Loading…
Reference in a new issue