Improve extract_msrv_attr! situation
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use crate::utils::{get_inner_attr, meets_msrv, snippet_opt, span_lint_and_then};
|
use crate::utils::{meets_msrv, snippet_opt, span_lint_and_then};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_ast::ast::{Attribute, Item, ItemKind, StructField, Variant, VariantData, VisibilityKind};
|
use rustc_ast::ast::{Attribute, Item, ItemKind, StructField, Variant, VariantData, VisibilityKind};
|
||||||
use rustc_attr as attr;
|
use rustc_attr as attr;
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
use crate::consts::{constant, Constant};
|
use crate::consts::{constant, Constant};
|
||||||
use crate::utils::usage::mutated_variables;
|
use crate::utils::usage::mutated_variables;
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
eq_expr_value, get_inner_attr, higher, match_def_path, meets_msrv, multispan_sugg, paths, qpath_res, snippet,
|
eq_expr_value, higher, match_def_path, meets_msrv, multispan_sugg, paths, qpath_res, snippet, span_lint_and_then,
|
||||||
span_lint_and_then,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_ast::ast::{Attribute, LitKind};
|
use rustc_ast::ast::LitKind;
|
||||||
use rustc_hir::def::Res;
|
use rustc_hir::def::Res;
|
||||||
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
||||||
use rustc_hir::BinOpKind;
|
use rustc_hir::BinOpKind;
|
||||||
|
|||||||
@@ -2,14 +2,14 @@ use crate::consts::{constant, miri_to_const, Constant};
|
|||||||
use crate::utils::sugg::Sugg;
|
use crate::utils::sugg::Sugg;
|
||||||
use crate::utils::usage::is_unused;
|
use crate::utils::usage::is_unused;
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
expr_block, get_arg_name, get_inner_attr, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of,
|
expr_block, get_arg_name, get_parent_expr, in_macro, indent_of, is_allowed, is_expn_of, is_refutable,
|
||||||
is_refutable, is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, meets_msrv, multispan_sugg,
|
is_type_diagnostic_item, is_wild, match_qpath, match_type, match_var, meets_msrv, multispan_sugg, remove_blocks,
|
||||||
remove_blocks, snippet, snippet_block, snippet_with_applicability, span_lint_and_help, span_lint_and_note,
|
snippet, snippet_block, snippet_with_applicability, span_lint_and_help, span_lint_and_note, span_lint_and_sugg,
|
||||||
span_lint_and_sugg, span_lint_and_then,
|
span_lint_and_then,
|
||||||
};
|
};
|
||||||
use crate::utils::{paths, search_same, SpanlessEq, SpanlessHash};
|
use crate::utils::{paths, search_same, SpanlessEq, SpanlessHash};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_ast::ast::{Attribute, LitKind};
|
use rustc_ast::ast::LitKind;
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::def::CtorKind;
|
use rustc_hir::def::CtorKind;
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ use std::iter;
|
|||||||
use bind_instead_of_map::BindInsteadOfMap;
|
use bind_instead_of_map::BindInsteadOfMap;
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_ast::ast;
|
use rustc_ast::ast;
|
||||||
use rustc_ast::ast::Attribute;
|
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::intravisit::{self, Visitor};
|
use rustc_hir::intravisit::{self, Visitor};
|
||||||
@@ -29,12 +28,11 @@ use crate::consts::{constant, Constant};
|
|||||||
use crate::utils::eager_or_lazy::is_lazyness_candidate;
|
use crate::utils::eager_or_lazy::is_lazyness_candidate;
|
||||||
use crate::utils::usage::mutated_variables;
|
use crate::utils::usage::mutated_variables;
|
||||||
use crate::utils::{
|
use crate::utils::{
|
||||||
contains_ty, get_arg_name, get_inner_attr, get_parent_expr, get_trait_def_id, has_iter_method, higher,
|
contains_ty, get_arg_name, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, in_macro,
|
||||||
implements_trait, in_macro, is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment,
|
is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, match_qpath,
|
||||||
match_def_path, match_qpath, match_trait_method, match_type, match_var, meets_msrv, method_calls,
|
match_trait_method, match_type, match_var, meets_msrv, method_calls, method_chain_args, paths, remove_blocks,
|
||||||
method_chain_args, paths, remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability,
|
return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
|
||||||
snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg,
|
span_lint_and_help, span_lint_and_sugg, span_lint_and_then, sugg, walk_ptrs_ty_depth, SpanlessEq,
|
||||||
walk_ptrs_ty_depth, SpanlessEq,
|
|
||||||
};
|
};
|
||||||
use semver::{Version, VersionReq};
|
use semver::{Version, VersionReq};
|
||||||
|
|
||||||
|
|||||||
@@ -79,30 +79,26 @@ pub fn meets_msrv(msrv: Option<&VersionReq>, lint_msrv: &Version) -> bool {
|
|||||||
msrv.map_or(true, |msrv| !msrv.matches(lint_msrv))
|
msrv.map_or(true, |msrv| !msrv.matches(lint_msrv))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
|
||||||
macro_rules! extract_msrv_attr {
|
macro_rules! extract_msrv_attr {
|
||||||
(LateContext) => {
|
(LateContext) => {
|
||||||
fn enter_lint_attrs(&mut self, cx: &rustc_lint::LateContext<'tcx>, attrs: &'tcx [Attribute]) {
|
extract_msrv_attr!(@LateContext, ());
|
||||||
match get_inner_attr(cx.sess(), attrs, "msrv") {
|
|
||||||
Some(msrv_attr) => {
|
|
||||||
if let Some(msrv) = msrv_attr.value_str() {
|
|
||||||
self.msrv = crate::utils::parse_msrv(&msrv.to_string(), Some(cx.sess()), Some(msrv_attr.span));
|
|
||||||
} else {
|
|
||||||
cx.sess().span_err(msrv_attr.span, "bad clippy attribute");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
(EarlyContext) => {
|
(EarlyContext) => {
|
||||||
fn enter_lint_attrs(&mut self, cx: &rustc_lint::EarlyContext<'tcx>, attrs: &'tcx [Attribute]) {
|
extract_msrv_attr!(@EarlyContext);
|
||||||
match get_inner_attr(cx.sess, attrs, "msrv") {
|
};
|
||||||
|
(@$context:ident$(, $call:tt)?) => {
|
||||||
|
fn enter_lint_attrs(&mut self, cx: &rustc_lint::$context<'tcx>, attrs: &'tcx [rustc_ast::ast::Attribute]) {
|
||||||
|
use $crate::utils::get_unique_inner_attr;
|
||||||
|
match get_unique_inner_attr(cx.sess$($call)?, attrs, "msrv") {
|
||||||
Some(msrv_attr) => {
|
Some(msrv_attr) => {
|
||||||
if let Some(msrv) = msrv_attr.value_str() {
|
if let Some(msrv) = msrv_attr.value_str() {
|
||||||
self.msrv = crate::utils::parse_msrv(&msrv.to_string(), Some(cx.sess), Some(msrv_attr.span));
|
self.msrv = $crate::utils::parse_msrv(
|
||||||
|
&msrv.to_string(),
|
||||||
|
Some(cx.sess$($call)?),
|
||||||
|
Some(msrv_attr.span),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
cx.sess.span_err(msrv_attr.span, "bad clippy attribute");
|
cx.sess$($call)?.span_err(msrv_attr.span, "bad clippy attribute");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => (),
|
_ => (),
|
||||||
|
|||||||
Reference in New Issue
Block a user