|
1 | 1 | use clippy_config::msrvs::Msrv; |
2 | 2 | use clippy_utils::diagnostics::span_lint; |
| 3 | +use clippy_utils::is_in_test_function; |
3 | 4 | use rustc_attr::{StabilityLevel, StableSince}; |
4 | 5 | use rustc_data_structures::fx::FxHashMap; |
5 | | -use rustc_hir::{Expr, ExprKind}; |
| 6 | +use rustc_hir::{Expr, ExprKind, HirId}; |
6 | 7 | use rustc_lint::{LateContext, LateLintPass}; |
7 | 8 | use rustc_middle::ty::TyCtxt; |
8 | 9 | use rustc_semver::RustcVersion; |
@@ -81,13 +82,13 @@ impl IncompatibleMsrv { |
81 | 82 | version |
82 | 83 | } |
83 | 84 |
|
84 | | - fn emit_lint_if_under_msrv(&mut self, cx: &LateContext<'_>, def_id: DefId, span: Span) { |
| 85 | + fn emit_lint_if_under_msrv(&mut self, cx: &LateContext<'_>, def_id: DefId, node: HirId, span: Span) { |
85 | 86 | if def_id.is_local() { |
86 | 87 | // We don't check local items since their MSRV is supposed to always be valid. |
87 | 88 | return; |
88 | 89 | } |
89 | 90 | let version = self.get_def_id_version(cx.tcx, def_id); |
90 | | - if self.msrv.meets(version) { |
| 91 | + if self.msrv.meets(version) || is_in_test_function(cx.tcx, node) { |
91 | 92 | return; |
92 | 93 | } |
93 | 94 | self.emit_lint_for(cx, span, version); |
@@ -117,14 +118,14 @@ impl<'tcx> LateLintPass<'tcx> for IncompatibleMsrv { |
117 | 118 | match expr.kind { |
118 | 119 | ExprKind::MethodCall(_, _, _, span) => { |
119 | 120 | if let Some(method_did) = cx.typeck_results().type_dependent_def_id(expr.hir_id) { |
120 | | - self.emit_lint_if_under_msrv(cx, method_did, span); |
| 121 | + self.emit_lint_if_under_msrv(cx, method_did, expr.hir_id, span); |
121 | 122 | } |
122 | 123 | }, |
123 | 124 | ExprKind::Call(call, [_]) => { |
124 | 125 | if let ExprKind::Path(qpath) = call.kind |
125 | 126 | && let Some(path_def_id) = cx.qpath_res(&qpath, call.hir_id).opt_def_id() |
126 | 127 | { |
127 | | - self.emit_lint_if_under_msrv(cx, path_def_id, call.span); |
| 128 | + self.emit_lint_if_under_msrv(cx, path_def_id, expr.hir_id, call.span); |
128 | 129 | } |
129 | 130 | }, |
130 | 131 | _ => {}, |
|
0 commit comments