Skip to content

Commit b1b4019

Browse files
committed
Improve extract_msrv_attr! situation
1 parent 4637236 commit b1b4019

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
lines changed

clippy_lints/src/manual_non_exhaustive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{get_inner_attr, meets_msrv, snippet_opt, span_lint_and_then};
1+
use crate::utils::{meets_msrv, snippet_opt, span_lint_and_then};
22
use if_chain::if_chain;
33
use rustc_ast::ast::{Attribute, Item, ItemKind, StructField, Variant, VariantData, VisibilityKind};
44
use rustc_attr as attr;

clippy_lints/src/manual_strip.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
use crate::consts::{constant, Constant};
22
use crate::utils::usage::mutated_variables;
33
use crate::utils::{
4-
eq_expr_value, get_inner_attr, higher, match_def_path, meets_msrv, multispan_sugg, paths, qpath_res, snippet,
5-
span_lint_and_then,
4+
eq_expr_value, higher, match_def_path, meets_msrv, multispan_sugg, paths, qpath_res, snippet, span_lint_and_then,
65
};
76

87
use if_chain::if_chain;
9-
use rustc_ast::ast::{Attribute, LitKind};
8+
use rustc_ast::ast::LitKind;
109
use rustc_hir::def::Res;
1110
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
1211
use rustc_hir::BinOpKind;

clippy_lints/src/matches.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ use crate::consts::{constant, miri_to_const, Constant};
22
use crate::utils::sugg::Sugg;
33
use crate::utils::usage::is_unused;
44
use crate::utils::{
5-
expr_block, get_arg_name, get_inner_attr, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of,
6-
is_refutable, is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, meets_msrv, multispan_sugg,
7-
remove_blocks, snippet, snippet_block, snippet_with_applicability, span_lint_and_help, span_lint_and_note,
8-
span_lint_and_sugg, span_lint_and_then,
5+
expr_block, get_arg_name, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of, is_refutable,
6+
is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, meets_msrv, multispan_sugg, remove_blocks,
7+
snippet, snippet_block, snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
8+
span_lint_and_then,
99
};
1010
use crate::utils::{paths, search_same, SpanlessEq, SpanlessHash};
1111
use if_chain::if_chain;
12-
use rustc_ast::ast::{Attribute, LitKind};
12+
use rustc_ast::ast::LitKind;
1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_errors::Applicability;
1515
use rustc_hir::def::CtorKind;

clippy_lints/src/methods/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use std::iter;
1212
use bind_instead_of_map::BindInsteadOfMap;
1313
use if_chain::if_chain;
1414
use rustc_ast::ast;
15-
use rustc_ast::ast::Attribute;
1615
use rustc_errors::Applicability;
1716
use rustc_hir as hir;
1817
use rustc_hir::intravisit::{self, Visitor};
@@ -29,12 +28,11 @@ use crate::consts::{constant, Constant};
2928
use crate::utils::eager_or_lazy::is_lazyness_candidate;
3029
use crate::utils::usage::mutated_variables;
3130
use crate::utils::{
32-
contains_ty, get_arg_name, get_inner_attr, get_parent_expr, get_trait_def_id, has_iter_method, higher,
33-
implements_trait, in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment,
34-
match_def_path, match_qpath, match_trait_method, match_type, match_var, meets_msrv, method_calls,
35-
method_chain_args, paths, remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability,
36-
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg,
37-
walk_ptrs_ty_depth, SpanlessEq,
31+
contains_ty, get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, in_macro,
32+
is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, match_qpath,
33+
match_trait_method, match_type, match_var, meets_msrv, method_calls, method_chain_args, paths, remove_blocks,
34+
return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
35+
span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg, walk_ptrs_ty_depth, SpanlessEq,
3836
};
3937
use semver::{Version, VersionReq};
4038

clippy_lints/src/utils/mod.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,24 @@ pub fn meets_msrv(msrv: Option<&VersionReq>, lint_msrv: &Version) -> bool {
8282
#[macro_export]
8383
macro_rules! extract_msrv_attr {
8484
(LateContext) => {
85-
fn enter_lint_attrs(&mut self, cx: &rustc_lint::LateContext<'tcx>, attrs: &'tcx [Attribute]) {
86-
match get_inner_attr(cx.sess(), attrs, "msrv") {
87-
Some(msrv_attr) => {
88-
if let Some(msrv) = msrv_attr.value_str() {
89-
self.msrv = crate::utils::parse_msrv(&msrv.to_string(), Some(cx.sess()), Some(msrv_attr.span));
90-
} else {
91-
cx.sess().span_err(msrv_attr.span, "bad clippy attribute");
92-
}
93-
},
94-
_ => (),
95-
}
96-
}
85+
extract_msrv_attr!(@LateContext, ());
9786
};
9887
(EarlyContext) => {
99-
fn enter_lint_attrs(&mut self, cx: &rustc_lint::EarlyContext<'tcx>, attrs: &'tcx [Attribute]) {
100-
match get_inner_attr(cx.sess, attrs, "msrv") {
88+
extract_msrv_attr!(@EarlyContext);
89+
};
90+
(@$context:ident$(, $call:tt)?) => {
91+
fn enter_lint_attrs(&mut self, cx: &rustc_lint::$context<'tcx>, attrs: &'tcx [rustc_ast::ast::Attribute]) {
92+
use $crate::utils::get_unique_inner_attr;
93+
match get_unique_inner_attr(cx.sess$($call)?, attrs, "msrv") {
10194
Some(msrv_attr) => {
10295
if let Some(msrv) = msrv_attr.value_str() {
103-
self.msrv = crate::utils::parse_msrv(&msrv.to_string(), Some(cx.sess), Some(msrv_attr.span));
96+
self.msrv = $crate::utils::parse_msrv(
97+
&msrv.to_string(),
98+
Some(cx.sess$($call)?),
99+
Some(msrv_attr.span),
100+
);
104101
} else {
105-
cx.sess.span_err(msrv_attr.span, "bad clippy attribute");
102+
cx.sess$($call)?.span_err(msrv_attr.span, "bad clippy attribute");
106103
}
107104
},
108105
_ => (),

0 commit comments

Comments
 (0)