Replace some Symbol::as_str usage (#14679)
Follow up to https://github.com/rust-lang/rust-clippy/pull/14650 Replaces uses in the form `s.as_str() == "literal"` r? @y21 changelog: none
This commit is contained in:
commit
0dd9722cdc
55 changed files with 448 additions and 179 deletions
|
|
@ -1,11 +1,11 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
use clippy_utils::ty::{has_non_owning_mutable_access, implements_trait};
|
||||
use clippy_utils::{is_mutable, is_trait_method, path_to_local};
|
||||
use clippy_utils::{is_mutable, is_trait_method, path_to_local, sym};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{Expr, Node, PatKind};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::Instance;
|
||||
use rustc_span::{Span, sym};
|
||||
use rustc_span::Span;
|
||||
|
||||
use super::DOUBLE_ENDED_ITERATOR_LAST;
|
||||
|
||||
|
|
@ -24,7 +24,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &'_ Expr<'_>, self_expr: &'_ Exp
|
|||
&& let Ok(Some(fn_def)) = Instance::try_resolve(cx.tcx, cx.typing_env(), id, args)
|
||||
// find the provided definition of Iterator::last
|
||||
&& let Some(item) = cx.tcx.get_diagnostic_item(sym::Iterator)
|
||||
&& let Some(last_def) = cx.tcx.provided_trait_methods(item).find(|m| m.name().as_str() == "last")
|
||||
&& let Some(last_def) = cx.tcx.provided_trait_methods(item).find(|m| m.name() == sym::last)
|
||||
// if the resolved method is the same as the provided definition
|
||||
&& fn_def.def_id() == last_def.def_id
|
||||
&& let self_ty = cx.typeck_results().expr_ty(self_expr)
|
||||
|
|
|
|||
|
|
@ -233,12 +233,12 @@ impl<'tcx> OffendingFilterExpr<'tcx> {
|
|||
// the latter only calls `effect` once
|
||||
let side_effect_expr_span = receiver.can_have_side_effects().then_some(receiver.span);
|
||||
|
||||
if cx.tcx.is_diagnostic_item(sym::Option, recv_ty.did()) && path.ident.name.as_str() == "is_some" {
|
||||
if cx.tcx.is_diagnostic_item(sym::Option, recv_ty.did()) && path.ident.name == sym::is_some {
|
||||
Some(Self::IsSome {
|
||||
receiver,
|
||||
side_effect_expr_span,
|
||||
})
|
||||
} else if cx.tcx.is_diagnostic_item(sym::Result, recv_ty.did()) && path.ident.name.as_str() == "is_ok" {
|
||||
} else if cx.tcx.is_diagnostic_item(sym::Result, recv_ty.did()) && path.ident.name == sym::is_ok {
|
||||
Some(Self::IsOk {
|
||||
receiver,
|
||||
side_effect_expr_span,
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use super::utils::get_last_chain_binding_hir_id;
|
|||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::paths::CHAR_IS_ASCII;
|
||||
use clippy_utils::source::SpanRangeExt;
|
||||
use clippy_utils::{match_def_path, path_to_local_id, peel_blocks};
|
||||
use clippy_utils::{match_def_path, path_to_local_id, peel_blocks, sym};
|
||||
|
||||
fn peels_expr_ref<'a, 'tcx>(mut expr: &'a Expr<'tcx>) -> &'a Expr<'tcx> {
|
||||
while let ExprKind::AddrOf(_, _, e) = expr.kind {
|
||||
|
|
@ -32,7 +32,7 @@ fn handle_expr(
|
|||
// If we have `!is_ascii`, then only `.any()` should warn. And if the condition is
|
||||
// `is_ascii`, then only `.all()` should warn.
|
||||
if revert != is_all
|
||||
&& method.ident.name.as_str() == "is_ascii"
|
||||
&& method.ident.name == sym::is_ascii
|
||||
&& path_to_local_id(receiver, first_param)
|
||||
&& let char_arg_ty = cx.typeck_results().expr_ty_adjusted(receiver).peel_refs()
|
||||
&& *char_arg_ty.kind() == ty::Char
|
||||
|
|
@ -102,7 +102,7 @@ pub(super) fn check(cx: &LateContext<'_>, call_expr: &Expr<'_>, recv: &Expr<'_>,
|
|||
&& let body = cx.tcx.hir_body(body)
|
||||
&& let Some(first_param) = body.params.first()
|
||||
&& let ExprKind::MethodCall(method, mut recv, [], _) = recv.kind
|
||||
&& method.ident.name.as_str() == "chars"
|
||||
&& method.ident.name == sym::chars
|
||||
&& let str_ty = cx.typeck_results().expr_ty_adjusted(recv).peel_refs()
|
||||
&& *str_ty.kind() == ty::Str
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use clippy_utils::ty::{
|
|||
};
|
||||
use clippy_utils::{
|
||||
CaptureKind, can_move_expr_to_closure, fn_def_id, get_enclosing_block, higher, is_trait_method, path_to_local,
|
||||
path_to_local_id,
|
||||
path_to_local_id, sym,
|
||||
};
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_errors::{Applicability, MultiSpan};
|
||||
|
|
@ -20,8 +20,8 @@ use rustc_hir::{
|
|||
use rustc_lint::LateContext;
|
||||
use rustc_middle::hir::nested_filter;
|
||||
use rustc_middle::ty::{self, AssocTag, ClauseKind, EarlyBinder, GenericArg, GenericArgKind, Ty};
|
||||
use rustc_span::Span;
|
||||
use rustc_span::symbol::Ident;
|
||||
use rustc_span::{Span, sym};
|
||||
|
||||
const NEEDLESS_COLLECT_MSG: &str = "avoid using `collect()` when not needed";
|
||||
|
||||
|
|
@ -339,7 +339,7 @@ impl<'tcx> Visitor<'tcx> for IterFunctionVisitor<'_, 'tcx> {
|
|||
// Check function calls on our collection
|
||||
if let ExprKind::MethodCall(method_name, recv, args, _) = &expr.kind {
|
||||
if args.is_empty()
|
||||
&& method_name.ident.name.as_str() == "collect"
|
||||
&& method_name.ident.name == sym::collect
|
||||
&& is_trait_method(self.cx, expr, sym::Iterator)
|
||||
{
|
||||
self.current_mutably_captured_ids = get_captured_ids(self.cx, self.cx.typeck_results().expr_ty(recv));
|
||||
|
|
|
|||
|
|
@ -1,17 +1,16 @@
|
|||
use std::ops::ControlFlow;
|
||||
|
||||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
use clippy_utils::get_parent_expr;
|
||||
use clippy_utils::source::snippet;
|
||||
use clippy_utils::ty::is_type_diagnostic_item;
|
||||
use clippy_utils::visitors::for_each_local_use_after_expr;
|
||||
use clippy_utils::{get_parent_expr, sym};
|
||||
use rustc_ast::LitKind;
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::def::Res;
|
||||
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, Ty};
|
||||
use rustc_span::sym;
|
||||
|
||||
use super::READ_LINE_WITHOUT_TRIM;
|
||||
|
||||
|
|
@ -44,7 +43,7 @@ pub fn check(cx: &LateContext<'_>, call: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<
|
|||
if let Some(parent) = get_parent_expr(cx, expr) {
|
||||
let data = if let ExprKind::MethodCall(segment, recv, args, span) = parent.kind {
|
||||
if args.is_empty()
|
||||
&& segment.ident.name.as_str() == "parse"
|
||||
&& segment.ident.name == sym::parse
|
||||
&& let parse_result_ty = cx.typeck_results().expr_ty(parent)
|
||||
&& is_type_diagnostic_item(cx, parse_result_ty, sym::Result)
|
||||
&& let ty::Adt(_, substs) = parse_result_ty.kind()
|
||||
|
|
@ -58,7 +57,7 @@ pub fn check(cx: &LateContext<'_>, call: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<
|
|||
"calling `.parse()` on a string without trimming the trailing newline character",
|
||||
"checking",
|
||||
))
|
||||
} else if segment.ident.name.as_str() == "ends_with"
|
||||
} else if segment.ident.name == sym::ends_with
|
||||
&& recv.span == expr.span
|
||||
&& let [arg] = args
|
||||
&& expr_is_string_literal_without_trailing_newline(arg)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||
use clippy_utils::source::snippet_with_context;
|
||||
use clippy_utils::sym;
|
||||
use clippy_utils::visitors::is_const_evaluatable;
|
||||
use rustc_ast::ast::LitKind;
|
||||
use rustc_errors::Applicability;
|
||||
|
|
@ -19,7 +20,7 @@ pub(super) fn check<'a>(cx: &LateContext<'a>, expr: &'_ Expr<'_>, split_recv: &'
|
|||
&& !is_const_evaluatable(cx, trim_recv)
|
||||
&& let ExprKind::Lit(split_lit) = split_arg.kind
|
||||
&& (matches!(split_lit.node, LitKind::Char('\n'))
|
||||
|| matches!(split_lit.node, LitKind::Str(sym, _) if (sym.as_str() == "\n" || sym.as_str() == "\r\n")))
|
||||
|| matches!(split_lit.node, LitKind::Str(sym::LF | sym::CRLF, _)))
|
||||
{
|
||||
let mut app = Applicability::MaybeIncorrect;
|
||||
span_lint_and_sugg(
|
||||
|
|
|
|||
|
|
@ -3,13 +3,12 @@ use clippy_utils::diagnostics::span_lint;
|
|||
use clippy_utils::ty::is_copy;
|
||||
use clippy_utils::usage::mutated_variables;
|
||||
use clippy_utils::visitors::{Descend, for_each_expr_without_closures};
|
||||
use clippy_utils::{is_res_lang_ctor, is_trait_method, path_res, path_to_local_id};
|
||||
use clippy_utils::{is_res_lang_ctor, is_trait_method, path_res, path_to_local_id, sym};
|
||||
use core::ops::ControlFlow;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty;
|
||||
use rustc_span::sym;
|
||||
|
||||
use super::{UNNECESSARY_FILTER_MAP, UNNECESSARY_FIND_MAP};
|
||||
|
||||
|
|
@ -95,7 +94,7 @@ fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tc
|
|||
(true, true)
|
||||
},
|
||||
hir::ExprKind::MethodCall(segment, recv, [arg], _) => {
|
||||
if segment.ident.name.as_str() == "then_some"
|
||||
if segment.ident.name == sym::then_some
|
||||
&& cx.typeck_results().expr_ty(recv).is_bool()
|
||||
&& path_to_local_id(arg, arg_id)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use clippy_utils::ty::{get_iterator_item_ty, implements_trait, is_copy, is_type_
|
|||
use clippy_utils::visitors::find_all_ret_expressions;
|
||||
use clippy_utils::{
|
||||
fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, is_expr_temporary_value, peel_middle_ty_refs,
|
||||
return_ty,
|
||||
return_ty, sym,
|
||||
};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::def::{DefKind, Res};
|
||||
|
|
@ -20,7 +20,7 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
|
|||
use rustc_middle::ty::{
|
||||
self, ClauseKind, GenericArg, GenericArgKind, GenericArgsRef, ParamTy, ProjectionPredicate, TraitPredicate, Ty,
|
||||
};
|
||||
use rustc_span::{Symbol, sym};
|
||||
use rustc_span::Symbol;
|
||||
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
|
||||
use rustc_trait_selection::traits::{Obligation, ObligationCause};
|
||||
|
||||
|
|
@ -312,8 +312,7 @@ fn check_string_from_utf8<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>,
|
|||
/// call of a `to_owned`-like function is unnecessary.
|
||||
fn check_split_call_arg(cx: &LateContext<'_>, expr: &Expr<'_>, method_name: Symbol, receiver: &Expr<'_>) -> bool {
|
||||
if let Some(parent) = get_parent_expr(cx, expr)
|
||||
&& let Some((fn_name, argument_expr)) = get_fn_name_and_arg(cx, parent)
|
||||
&& fn_name.as_str() == "split"
|
||||
&& let Some((sym::split, argument_expr)) = get_fn_name_and_arg(cx, parent)
|
||||
&& let Some(receiver_snippet) = receiver.span.get_source_text(cx)
|
||||
&& let Some(arg_snippet) = argument_expr.span.get_source_text(cx)
|
||||
{
|
||||
|
|
@ -614,8 +613,7 @@ fn has_lifetime(ty: Ty<'_>) -> bool {
|
|||
|
||||
/// Returns true if the named method is `Iterator::cloned` or `Iterator::copied`.
|
||||
fn is_cloned_or_copied(cx: &LateContext<'_>, method_name: Symbol, method_def_id: DefId) -> bool {
|
||||
(method_name.as_str() == "cloned" || method_name.as_str() == "copied")
|
||||
&& is_diag_trait_item(cx, method_def_id, sym::Iterator)
|
||||
matches!(method_name, sym::cloned | sym::copied) && is_diag_trait_item(cx, method_def_id, sym::Iterator)
|
||||
}
|
||||
|
||||
/// Returns true if the named method can be used to convert the receiver to its "owned"
|
||||
|
|
@ -628,7 +626,7 @@ fn is_to_owned_like<'a>(cx: &LateContext<'a>, call_expr: &Expr<'a>, method_name:
|
|||
|
||||
/// Returns true if the named method is `Cow::into_owned`.
|
||||
fn is_cow_into_owned(cx: &LateContext<'_>, method_name: Symbol, method_def_id: DefId) -> bool {
|
||||
method_name.as_str() == "into_owned" && is_diag_item_method(cx, method_def_id, sym::Cow)
|
||||
method_name == sym::into_owned && is_diag_item_method(cx, method_def_id, sym::Cow)
|
||||
}
|
||||
|
||||
/// Returns true if the named method is `ToString::to_string` and it's called on a type that
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue