mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-11 08:05:12 +00:00
Cleanup: Use our sym!
macro more
It's much shorter that Symbol::intern and the result should still be clear.
This commit is contained in:
parent
e29d550565
commit
31c5664f25
@ -8,7 +8,6 @@ use rustc_hir::{BinOpKind, Expr, ExprKind};
|
|||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::source_map::Spanned;
|
use rustc_span::source_map::Spanned;
|
||||||
use rustc_span::symbol::Symbol;
|
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
/// **What it does:** Checks for using `x.get(x.len() - 1)` instead of
|
/// **What it does:** Checks for using `x.get(x.len() - 1)` instead of
|
||||||
@ -51,12 +50,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for GetLastWithLen {
|
|||||||
if let ExprKind::MethodCall(ref path, _, ref args) = expr.kind;
|
if let ExprKind::MethodCall(ref path, _, ref args) = expr.kind;
|
||||||
|
|
||||||
// Method name is "get"
|
// Method name is "get"
|
||||||
if path.ident.name == Symbol::intern("get");
|
if path.ident.name == sym!(get);
|
||||||
|
|
||||||
// Argument 0 (the struct we're calling the method on) is a vector
|
// Argument 0 (the struct we're calling the method on) is a vector
|
||||||
if let Some(struct_calling_on) = args.get(0);
|
if let Some(struct_calling_on) = args.get(0);
|
||||||
let struct_ty = cx.tables.expr_ty(struct_calling_on);
|
let struct_ty = cx.tables.expr_ty(struct_calling_on);
|
||||||
if is_type_diagnostic_item(cx, struct_ty, Symbol::intern("vec_type"));
|
if is_type_diagnostic_item(cx, struct_ty, sym!(vec_type));
|
||||||
|
|
||||||
// Argument to "get" is a subtraction
|
// Argument to "get" is a subtraction
|
||||||
if let Some(get_index_arg) = args.get(1);
|
if let Some(get_index_arg) = args.get(1);
|
||||||
@ -71,7 +70,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for GetLastWithLen {
|
|||||||
|
|
||||||
// LHS of subtraction is "x.len()"
|
// LHS of subtraction is "x.len()"
|
||||||
if let ExprKind::MethodCall(arg_lhs_path, _, lhs_args) = &lhs.kind;
|
if let ExprKind::MethodCall(arg_lhs_path, _, lhs_args) = &lhs.kind;
|
||||||
if arg_lhs_path.ident.name == Symbol::intern("len");
|
if arg_lhs_path.ident.name == sym!(len);
|
||||||
if let Some(arg_lhs_struct) = lhs_args.get(0);
|
if let Some(arg_lhs_struct) = lhs_args.get(0);
|
||||||
|
|
||||||
// The two vectors referenced (x in x.get(...) and in x.len())
|
// The two vectors referenced (x in x.get(...) and in x.len())
|
||||||
|
@ -28,7 +28,7 @@ use rustc_middle::middle::region;
|
|||||||
use rustc_middle::ty::{self, Ty};
|
use rustc_middle::ty::{self, Ty};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::source_map::Span;
|
use rustc_span::source_map::Span;
|
||||||
use rustc_span::{BytePos, Symbol};
|
use rustc_span::BytePos;
|
||||||
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, Place, PlaceBase};
|
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, Place, PlaceBase};
|
||||||
use std::iter::{once, Iterator};
|
use std::iter::{once, Iterator};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
@ -804,7 +804,7 @@ fn is_slice_like<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, ty: Ty<'_>) -> bool {
|
|||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
|
|
||||||
is_slice || is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) || match_type(cx, ty, &paths::VEC_DEQUE)
|
is_slice || is_type_diagnostic_item(cx, ty, sym!(vec_type)) || match_type(cx, ty, &paths::VEC_DEQUE)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr<'_>, var: HirId) -> Option<FixedOffsetVar> {
|
fn get_fixed_offset_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr<'_>, var: HirId) -> Option<FixedOffsetVar> {
|
||||||
@ -1955,7 +1955,7 @@ fn is_ref_iterable_type(cx: &LateContext<'_, '_>, e: &Expr<'_>) -> bool {
|
|||||||
// will allow further borrows afterwards
|
// will allow further borrows afterwards
|
||||||
let ty = cx.tables.expr_ty(e);
|
let ty = cx.tables.expr_ty(e);
|
||||||
is_iterable_array(ty, cx) ||
|
is_iterable_array(ty, cx) ||
|
||||||
is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) ||
|
is_type_diagnostic_item(cx, ty, sym!(vec_type)) ||
|
||||||
match_type(cx, ty, &paths::LINKED_LIST) ||
|
match_type(cx, ty, &paths::LINKED_LIST) ||
|
||||||
match_type(cx, ty, &paths::HASHMAP) ||
|
match_type(cx, ty, &paths::HASHMAP) ||
|
||||||
match_type(cx, ty, &paths::HASHSET) ||
|
match_type(cx, ty, &paths::HASHSET) ||
|
||||||
@ -2465,7 +2465,7 @@ fn check_needless_collect<'a, 'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'a, '
|
|||||||
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
|
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
|
||||||
then {
|
then {
|
||||||
let ty = cx.tables.node_type(ty.hir_id);
|
let ty = cx.tables.node_type(ty.hir_id);
|
||||||
if is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) ||
|
if is_type_diagnostic_item(cx, ty, sym!(vec_type)) ||
|
||||||
match_type(cx, ty, &paths::VEC_DEQUE) ||
|
match_type(cx, ty, &paths::VEC_DEQUE) ||
|
||||||
match_type(cx, ty, &paths::BTREEMAP) ||
|
match_type(cx, ty, &paths::BTREEMAP) ||
|
||||||
match_type(cx, ty, &paths::HASHMAP) {
|
match_type(cx, ty, &paths::HASHMAP) {
|
||||||
|
@ -19,7 +19,7 @@ use rustc_middle::ty::subst::GenericArgKind;
|
|||||||
use rustc_middle::ty::{self, Predicate, Ty};
|
use rustc_middle::ty::{self, Predicate, Ty};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::source_map::Span;
|
use rustc_span::source_map::Span;
|
||||||
use rustc_span::symbol::{sym, Symbol, SymbolStr};
|
use rustc_span::symbol::{sym, SymbolStr};
|
||||||
|
|
||||||
use crate::consts::{constant, Constant};
|
use crate::consts::{constant, Constant};
|
||||||
use crate::utils::usage::mutated_variables;
|
use crate::utils::usage::mutated_variables;
|
||||||
@ -2089,7 +2089,7 @@ fn lint_iter_cloned_collect<'a, 'tcx>(
|
|||||||
iter_args: &'tcx [hir::Expr<'_>],
|
iter_args: &'tcx [hir::Expr<'_>],
|
||||||
) {
|
) {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if is_type_diagnostic_item(cx, cx.tables.expr_ty(expr), Symbol::intern("vec_type"));
|
if is_type_diagnostic_item(cx, cx.tables.expr_ty(expr), sym!(vec_type));
|
||||||
if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0]));
|
if let Some(slice) = derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0]));
|
||||||
if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
|
if let Some(to_replace) = expr.span.trim_start(slice.span.source_callsite());
|
||||||
|
|
||||||
@ -2218,7 +2218,7 @@ fn lint_iter_nth<'a, 'tcx>(
|
|||||||
let mut_str = if is_mut { "_mut" } else { "" };
|
let mut_str = if is_mut { "_mut" } else { "" };
|
||||||
let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0])).is_some() {
|
let caller_type = if derefs_to_slice(cx, &iter_args[0], cx.tables.expr_ty(&iter_args[0])).is_some() {
|
||||||
"slice"
|
"slice"
|
||||||
} else if is_type_diagnostic_item(cx, cx.tables.expr_ty(&iter_args[0]), Symbol::intern("vec_type")) {
|
} else if is_type_diagnostic_item(cx, cx.tables.expr_ty(&iter_args[0]), sym!(vec_type)) {
|
||||||
"Vec"
|
"Vec"
|
||||||
} else if match_type(cx, cx.tables.expr_ty(&iter_args[0]), &paths::VEC_DEQUE) {
|
} else if match_type(cx, cx.tables.expr_ty(&iter_args[0]), &paths::VEC_DEQUE) {
|
||||||
"VecDeque"
|
"VecDeque"
|
||||||
@ -2275,7 +2275,7 @@ fn lint_get_unwrap<'a, 'tcx>(
|
|||||||
let caller_type = if derefs_to_slice(cx, &get_args[0], expr_ty).is_some() {
|
let caller_type = if derefs_to_slice(cx, &get_args[0], expr_ty).is_some() {
|
||||||
needs_ref = get_args_str.parse::<usize>().is_ok();
|
needs_ref = get_args_str.parse::<usize>().is_ok();
|
||||||
"slice"
|
"slice"
|
||||||
} else if is_type_diagnostic_item(cx, expr_ty, Symbol::intern("vec_type")) {
|
} else if is_type_diagnostic_item(cx, expr_ty, sym!(vec_type)) {
|
||||||
needs_ref = get_args_str.parse::<usize>().is_ok();
|
needs_ref = get_args_str.parse::<usize>().is_ok();
|
||||||
"Vec"
|
"Vec"
|
||||||
} else if match_type(cx, expr_ty, &paths::VEC_DEQUE) {
|
} else if match_type(cx, expr_ty, &paths::VEC_DEQUE) {
|
||||||
@ -2356,7 +2356,7 @@ fn derefs_to_slice<'a, 'tcx>(
|
|||||||
match ty.kind {
|
match ty.kind {
|
||||||
ty::Slice(_) => true,
|
ty::Slice(_) => true,
|
||||||
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
|
ty::Adt(def, _) if def.is_box() => may_slice(cx, ty.boxed_ty()),
|
||||||
ty::Adt(..) => is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")),
|
ty::Adt(..) => is_type_diagnostic_item(cx, ty, sym!(vec_type)),
|
||||||
ty::Array(_, size) => {
|
ty::Array(_, size) => {
|
||||||
if let Some(size) = size.try_eval_usize(cx.tcx, cx.param_env) {
|
if let Some(size) = size.try_eval_usize(cx.tcx, cx.param_env) {
|
||||||
size < 32
|
size < 32
|
||||||
|
@ -13,7 +13,7 @@ use rustc_infer::infer::TyCtxtInferExt;
|
|||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty::{self, TypeFoldable};
|
use rustc_middle::ty::{self, TypeFoldable};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::{Span, Symbol};
|
use rustc_span::Span;
|
||||||
use rustc_target::spec::abi::Abi;
|
use rustc_target::spec::abi::Abi;
|
||||||
use rustc_trait_selection::traits;
|
use rustc_trait_selection::traits;
|
||||||
use rustc_trait_selection::traits::misc::can_type_implement_copy;
|
use rustc_trait_selection::traits::misc::can_type_implement_copy;
|
||||||
@ -214,7 +214,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
|
|||||||
|
|
||||||
let deref_span = spans_need_deref.get(&canonical_id);
|
let deref_span = spans_need_deref.get(&canonical_id);
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type"));
|
if is_type_diagnostic_item(cx, ty, sym!(vec_type));
|
||||||
if let Some(clone_spans) =
|
if let Some(clone_spans) =
|
||||||
get_spans(cx, Some(body.id()), idx, &[("clone", ".to_owned()")]);
|
get_spans(cx, Some(body.id()), idx, &[("clone", ".to_owned()")]);
|
||||||
if let TyKind::Path(QPath::Resolved(_, ref path)) = input.kind;
|
if let TyKind::Path(QPath::Resolved(_, ref path)) = input.kind;
|
||||||
|
@ -15,7 +15,7 @@ use rustc_lint::{LateContext, LateLintPass};
|
|||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::source_map::Span;
|
use rustc_span::source_map::Span;
|
||||||
use rustc_span::{MultiSpan, Symbol};
|
use rustc_span::MultiSpan;
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
@ -153,7 +153,7 @@ fn check_fn(cx: &LateContext<'_, '_>, decl: &FnDecl<'_>, fn_id: HirId, opt_body_
|
|||||||
|
|
||||||
for (idx, (arg, ty)) in decl.inputs.iter().zip(fn_ty.inputs()).enumerate() {
|
for (idx, (arg, ty)) in decl.inputs.iter().zip(fn_ty.inputs()).enumerate() {
|
||||||
if let ty::Ref(_, ty, Mutability::Not) = ty.kind {
|
if let ty::Ref(_, ty, Mutability::Not) = ty.kind {
|
||||||
if is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type")) {
|
if is_type_diagnostic_item(cx, ty, sym!(vec_type)) {
|
||||||
let mut ty_snippet = None;
|
let mut ty_snippet = None;
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let TyKind::Path(QPath::Resolved(_, ref path)) = walk_ptrs_hir_ty(arg).kind;
|
if let TyKind::Path(QPath::Resolved(_, ref path)) = walk_ptrs_hir_ty(arg).kind;
|
||||||
|
@ -9,7 +9,6 @@ use rustc_hir::{Block, Expr, ExprKind, PatKind, QPath, StmtKind};
|
|||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::Symbol;
|
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
/// **What it does:** Checks for manual swapping.
|
/// **What it does:** Checks for manual swapping.
|
||||||
@ -199,7 +198,7 @@ fn check_for_slice<'a>(cx: &LateContext<'_, '_>, lhs1: &'a Expr<'_>, lhs2: &'a E
|
|||||||
|
|
||||||
if matches!(ty.kind, ty::Slice(_))
|
if matches!(ty.kind, ty::Slice(_))
|
||||||
|| matches!(ty.kind, ty::Array(_, _))
|
|| matches!(ty.kind, ty::Array(_, _))
|
||||||
|| is_type_diagnostic_item(cx, ty, Symbol::intern("vec_type"))
|
|| is_type_diagnostic_item(cx, ty, sym!(vec_type))
|
||||||
|| match_type(cx, ty, &paths::VEC_DEQUE)
|
|| match_type(cx, ty, &paths::VEC_DEQUE)
|
||||||
{
|
{
|
||||||
return Slice::Swappable(lhs1, idx1, idx2);
|
return Slice::Swappable(lhs1, idx1, idx2);
|
||||||
|
@ -21,7 +21,7 @@ use rustc_middle::ty::{self, InferTy, Ty, TyCtxt, TypeckTables};
|
|||||||
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
|
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
|
||||||
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
use rustc_span::hygiene::{ExpnKind, MacroKind};
|
||||||
use rustc_span::source_map::Span;
|
use rustc_span::source_map::Span;
|
||||||
use rustc_span::symbol::{sym, Symbol};
|
use rustc_span::symbol::sym;
|
||||||
use rustc_target::abi::LayoutOf;
|
use rustc_target::abi::LayoutOf;
|
||||||
use rustc_target::spec::abi::Abi;
|
use rustc_target::spec::abi::Abi;
|
||||||
use rustc_typeck::hir_ty_to_ty;
|
use rustc_typeck::hir_ty_to_ty;
|
||||||
@ -384,7 +384,7 @@ impl Types {
|
|||||||
);
|
);
|
||||||
return; // don't recurse into the type
|
return; // don't recurse into the type
|
||||||
}
|
}
|
||||||
} else if cx.tcx.is_diagnostic_item(Symbol::intern("vec_type"), def_id) {
|
} else if cx.tcx.is_diagnostic_item(sym!(vec_type), def_id) {
|
||||||
if_chain! {
|
if_chain! {
|
||||||
// Get the _ part of Vec<_>
|
// Get the _ part of Vec<_>
|
||||||
if let Some(ref last) = last_path_segment(qpath).args;
|
if let Some(ref last) = last_path_segment(qpath).args;
|
||||||
|
Loading…
Reference in New Issue
Block a user