mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-17 06:28:42 +00:00
Fix macro expansion in try_err lint
This commit is contained in:
parent
b041511b5f
commit
0487b58f9a
4 changed files with 63 additions and 5 deletions
|
@ -1,10 +1,11 @@
|
||||||
use crate::utils::{match_qpath, paths, snippet, span_lint_and_sugg};
|
use crate::utils::{in_macro_or_desugar, 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};
|
||||||
use rustc::ty::Ty;
|
use rustc::ty::Ty;
|
||||||
use rustc::{declare_lint_pass, declare_tool_lint};
|
use rustc::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
|
use syntax::source_map::Span;
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
/// **What it does:** Checks for usages of `Err(x)?`.
|
/// **What it does:** Checks for usages of `Err(x)?`.
|
||||||
|
@ -67,10 +68,15 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
|
||||||
|
|
||||||
then {
|
then {
|
||||||
let err_type = cx.tables.expr_ty(err_arg);
|
let err_type = cx.tables.expr_ty(err_arg);
|
||||||
let suggestion = if err_type == return_type {
|
let span = if in_macro_or_desugar(err_arg.span) {
|
||||||
format!("return Err({})", snippet(cx, err_arg.span, "_"))
|
span_to_outer_expn(err_arg.span)
|
||||||
} else {
|
} else {
|
||||||
format!("return Err({}.into())", snippet(cx, err_arg.span, "_"))
|
err_arg.span
|
||||||
|
};
|
||||||
|
let suggestion = if err_type == return_type {
|
||||||
|
format!("return Err({})", snippet(cx, span, "_"))
|
||||||
|
} else {
|
||||||
|
format!("return Err({}.into())", snippet(cx, span, "_"))
|
||||||
};
|
};
|
||||||
|
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
|
@ -87,6 +93,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TryErr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn span_to_outer_expn(span: Span) -> Span {
|
||||||
|
let mut span = span;
|
||||||
|
while let Some(expr) = span.ctxt().outer_expn_info() {
|
||||||
|
span = expr.call_site;
|
||||||
|
}
|
||||||
|
span
|
||||||
|
}
|
||||||
|
|
||||||
// In order to determine whether to suggest `.into()` or not, we need to find the error type the
|
// In order to determine whether to suggest `.into()` or not, we need to find the error type the
|
||||||
// function returns. To do that, we look for the From::from call (see tree above), and capture
|
// function returns. To do that, we look for the From::from call (see tree above), and capture
|
||||||
// its output type.
|
// its output type.
|
||||||
|
|
|
@ -78,3 +78,22 @@ fn main() {
|
||||||
closure_matches_test().unwrap();
|
closure_matches_test().unwrap();
|
||||||
closure_into_test().unwrap();
|
closure_into_test().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! bar {
|
||||||
|
() => {
|
||||||
|
String::from("aasdfasdfasdfa")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! foo {
|
||||||
|
() => {
|
||||||
|
bar!()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn macro_inside(fail: bool) -> Result<i32, String> {
|
||||||
|
if fail {
|
||||||
|
return Err(foo!());
|
||||||
|
}
|
||||||
|
Ok(0)
|
||||||
|
}
|
||||||
|
|
|
@ -78,3 +78,22 @@ fn main() {
|
||||||
closure_matches_test().unwrap();
|
closure_matches_test().unwrap();
|
||||||
closure_into_test().unwrap();
|
closure_into_test().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! bar {
|
||||||
|
() => {
|
||||||
|
String::from("aasdfasdfasdfa")
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! foo {
|
||||||
|
() => {
|
||||||
|
bar!()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn macro_inside(fail: bool) -> Result<i32, String> {
|
||||||
|
if fail {
|
||||||
|
Err(foo!())?;
|
||||||
|
}
|
||||||
|
Ok(0)
|
||||||
|
}
|
||||||
|
|
|
@ -28,5 +28,11 @@ error: returning an `Err(_)` with the `?` operator
|
||||||
LL | Err(err)?;
|
LL | Err(err)?;
|
||||||
| ^^^^^^^^^ help: try this: `return Err(err.into())`
|
| ^^^^^^^^^ help: try this: `return Err(err.into())`
|
||||||
|
|
||||||
error: aborting due to 4 previous errors
|
error: returning an `Err(_)` with the `?` operator
|
||||||
|
--> $DIR/try_err.rs:96:9
|
||||||
|
|
|
||||||
|
LL | Err(foo!())?;
|
||||||
|
| ^^^^^^^^^^^^ help: try this: `return Err(foo!())`
|
||||||
|
|
||||||
|
error: aborting due to 5 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue