From 52f52900a4dc90c6727432863c2c66aecbeea69f Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Thu, 24 Oct 2019 07:52:01 +0200 Subject: [PATCH] Don't emit try_err lint in external macros --- clippy_lints/src/try_err.rs | 3 ++- tests/ui/auxiliary/macro_rules.rs | 15 +++++++++++++++ tests/ui/try_err.fixed | 7 +++++++ tests/ui/try_err.rs | 7 +++++++ tests/ui/try_err.stderr | 12 ++++++------ 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/clippy_lints/src/try_err.rs b/clippy_lints/src/try_err.rs index 35592a1ce50..0985f966e8e 100644 --- a/clippy_lints/src/try_err.rs +++ b/clippy_lints/src/try_err.rs @@ -1,7 +1,7 @@ use crate::utils::{match_qpath, paths, snippet, snippet_with_macro_callsite, span_lint_and_sugg}; use if_chain::if_chain; use rustc::hir::*; -use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; +use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintPass}; use rustc::ty::Ty; use rustc::{declare_lint_pass, declare_tool_lint}; use rustc_errors::Applicability; @@ -54,6 +54,7 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { // val, // }; if_chain! { + if !in_external_macro(cx.tcx.sess, expr.span); if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.kind; if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.kind; if let ExprKind::Path(ref match_fun_path) = match_fun.kind; diff --git a/tests/ui/auxiliary/macro_rules.rs b/tests/ui/auxiliary/macro_rules.rs index b717afd0b27..002b05b588d 100644 --- a/tests/ui/auxiliary/macro_rules.rs +++ b/tests/ui/auxiliary/macro_rules.rs @@ -16,3 +16,18 @@ macro_rules! must_use_unit { fn foo() {} }; } + +#[macro_export] +macro_rules! try_err { + () => { + pub fn try_err_fn() -> Result { + let err: i32 = 1; + // To avoid warnings during rustfix + if true { + Err(err)? + } else { + Ok(2) + } + } + }; +} diff --git a/tests/ui/try_err.fixed b/tests/ui/try_err.fixed index a2087316e37..29d9139d3e3 100644 --- a/tests/ui/try_err.fixed +++ b/tests/ui/try_err.fixed @@ -1,7 +1,11 @@ // run-rustfix +// aux-build:macro_rules.rs #![deny(clippy::try_err)] +#[macro_use] +extern crate macro_rules; + // Tests that a simple case works // Should flag `Err(err)?` pub fn basic_test() -> Result { @@ -77,6 +81,9 @@ fn main() { negative_test().unwrap(); closure_matches_test().unwrap(); closure_into_test().unwrap(); + + // We don't want to lint in external macros + try_err!(); } macro_rules! bar { diff --git a/tests/ui/try_err.rs b/tests/ui/try_err.rs index 5ef1b615dc7..5e85d091a2a 100644 --- a/tests/ui/try_err.rs +++ b/tests/ui/try_err.rs @@ -1,7 +1,11 @@ // run-rustfix +// aux-build:macro_rules.rs #![deny(clippy::try_err)] +#[macro_use] +extern crate macro_rules; + // Tests that a simple case works // Should flag `Err(err)?` pub fn basic_test() -> Result { @@ -77,6 +81,9 @@ fn main() { negative_test().unwrap(); closure_matches_test().unwrap(); closure_into_test().unwrap(); + + // We don't want to lint in external macros + try_err!(); } macro_rules! bar { diff --git a/tests/ui/try_err.stderr b/tests/ui/try_err.stderr index b915d6b601d..dbd910f5624 100644 --- a/tests/ui/try_err.stderr +++ b/tests/ui/try_err.stderr @@ -1,35 +1,35 @@ error: returning an `Err(_)` with the `?` operator - --> $DIR/try_err.rs:11:9 + --> $DIR/try_err.rs:15:9 | LL | Err(err)?; | ^^^^^^^^^ help: try this: `return Err(err)` | note: lint level defined here - --> $DIR/try_err.rs:3:9 + --> $DIR/try_err.rs:4:9 | LL | #![deny(clippy::try_err)] | ^^^^^^^^^^^^^^^ error: returning an `Err(_)` with the `?` operator - --> $DIR/try_err.rs:21:9 + --> $DIR/try_err.rs:25:9 | LL | Err(err)?; | ^^^^^^^^^ help: try this: `return Err(err.into())` error: returning an `Err(_)` with the `?` operator - --> $DIR/try_err.rs:41:17 + --> $DIR/try_err.rs:45:17 | LL | Err(err)?; | ^^^^^^^^^ help: try this: `return Err(err)` error: returning an `Err(_)` with the `?` operator - --> $DIR/try_err.rs:60:17 + --> $DIR/try_err.rs:64:17 | LL | Err(err)?; | ^^^^^^^^^ help: try this: `return Err(err.into())` error: returning an `Err(_)` with the `?` operator - --> $DIR/try_err.rs:96:9 + --> $DIR/try_err.rs:103:9 | LL | Err(foo!())?; | ^^^^^^^^^^^^ help: try this: `return Err(foo!())`