mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-17 06:28:42 +00:00
Rework ast::BinOpKind::to_string
and ast::UnOp::to_string
.
- Rename them both `as_str`, which is the typical name for a function that returns a `&str`. (`to_string` is appropriate for functions returning `String` or maybe `Cow<'a, str>`.) - Change `UnOp::as_str` from an associated function (weird!) to a method. - Avoid needless `self` dereferences.
This commit is contained in:
parent
bf86fe130c
commit
684c4bfef1
4 changed files with 10 additions and 10 deletions
|
@ -1,7 +1,7 @@
|
|||
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
|
||||
use clippy_utils::is_span_if;
|
||||
use clippy_utils::source::snippet_opt;
|
||||
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
|
||||
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind};
|
||||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
||||
use rustc_middle::lint::in_external_macro;
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
|
@ -144,7 +144,7 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
|
|||
let eq_span = lhs.span.between(rhs.span);
|
||||
if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind {
|
||||
if let Some(eq_snippet) = snippet_opt(cx, eq_span) {
|
||||
let op = UnOp::to_string(op);
|
||||
let op = op.as_str();
|
||||
let eqop_span = lhs.span.between(sub_rhs.span);
|
||||
if eq_snippet.ends_with('=') {
|
||||
span_lint_and_note(
|
||||
|
@ -177,11 +177,11 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
|
|||
&& let unop_operand_span = rhs.span.until(un_rhs.span)
|
||||
&& let Some(binop_snippet) = snippet_opt(cx, binop_span)
|
||||
&& let Some(unop_operand_snippet) = snippet_opt(cx, unop_operand_span)
|
||||
&& let binop_str = BinOpKind::to_string(&binop.node)
|
||||
&& let binop_str = binop.node.as_str()
|
||||
// no space after BinOp operator and space after UnOp operator
|
||||
&& binop_snippet.ends_with(binop_str) && unop_operand_snippet.ends_with(' ')
|
||||
{
|
||||
let unop_str = UnOp::to_string(op);
|
||||
let unop_str = op.as_str();
|
||||
let eqop_span = lhs.span.between(un_rhs.span);
|
||||
span_lint_and_help(
|
||||
cx,
|
||||
|
|
|
@ -78,7 +78,7 @@ impl EarlyLintPass for Precedence {
|
|||
let sugg = format!(
|
||||
"({}) {} ({})",
|
||||
snippet_with_applicability(cx, left.span, "..", &mut applicability),
|
||||
op.to_string(),
|
||||
op.as_str(),
|
||||
snippet_with_applicability(cx, right.span, "..", &mut applicability)
|
||||
);
|
||||
span_sugg(expr, sugg, applicability);
|
||||
|
@ -87,7 +87,7 @@ impl EarlyLintPass for Precedence {
|
|||
let sugg = format!(
|
||||
"({}) {} {}",
|
||||
snippet_with_applicability(cx, left.span, "..", &mut applicability),
|
||||
op.to_string(),
|
||||
op.as_str(),
|
||||
snippet_with_applicability(cx, right.span, "..", &mut applicability)
|
||||
);
|
||||
span_sugg(expr, sugg, applicability);
|
||||
|
@ -96,7 +96,7 @@ impl EarlyLintPass for Precedence {
|
|||
let sugg = format!(
|
||||
"{} {} ({})",
|
||||
snippet_with_applicability(cx, left.span, "..", &mut applicability),
|
||||
op.to_string(),
|
||||
op.as_str(),
|
||||
snippet_with_applicability(cx, right.span, "..", &mut applicability)
|
||||
);
|
||||
span_sugg(expr, sugg, applicability);
|
||||
|
|
|
@ -298,7 +298,7 @@ fn replace_left_sugg(
|
|||
) -> String {
|
||||
format!(
|
||||
"{left_suggestion} {} {}",
|
||||
binop.op.to_string(),
|
||||
binop.op.as_str(),
|
||||
snippet_with_applicability(cx, binop.right.span, "..", applicability),
|
||||
)
|
||||
}
|
||||
|
@ -312,7 +312,7 @@ fn replace_right_sugg(
|
|||
format!(
|
||||
"{} {} {right_suggestion}",
|
||||
snippet_with_applicability(cx, binop.left.span, "..", applicability),
|
||||
binop.op.to_string(),
|
||||
binop.op.as_str(),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -382,7 +382,7 @@ fn binop_to_string(op: AssocOp, lhs: &str, rhs: &str) -> String {
|
|||
| AssocOp::GreaterEqual => {
|
||||
format!(
|
||||
"{lhs} {} {rhs}",
|
||||
op.to_ast_binop().expect("Those are AST ops").to_string()
|
||||
op.to_ast_binop().expect("Those are AST ops").as_str()
|
||||
)
|
||||
},
|
||||
AssocOp::Assign => format!("{lhs} = {rhs}"),
|
||||
|
|
Loading…
Add table
Reference in a new issue