mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Auto merge of #4355 - lzutao:macro_expn_try_err, r=flip1995
Fix macro expansion in try_err lint Fixes #4309 changelog: none
This commit is contained in:
commit
4465e2fbb8
@ -1,4 +1,4 @@
|
|||||||
use crate::utils::{match_qpath, paths, snippet, span_lint_and_sugg};
|
use crate::utils::{in_macro_or_desugar, match_qpath, paths, snippet, snippet_with_macro_callsite, 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};
|
||||||
@ -67,10 +67,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 origin_snippet = if in_macro_or_desugar(err_arg.span) {
|
||||||
format!("return Err({})", snippet(cx, err_arg.span, "_"))
|
snippet_with_macro_callsite(cx, err_arg.span, "_")
|
||||||
} else {
|
} else {
|
||||||
format!("return Err({}.into())", snippet(cx, err_arg.span, "_"))
|
snippet(cx, err_arg.span, "_")
|
||||||
|
};
|
||||||
|
let suggestion = if err_type == return_type {
|
||||||
|
format!("return Err({})", origin_snippet)
|
||||||
|
} else {
|
||||||
|
format!("return Err({}.into())", origin_snippet)
|
||||||
};
|
};
|
||||||
|
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
|
@ -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…
Reference in New Issue
Block a user