Skip to content

Commit 684c4bf

Browse files
committed
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.
1 parent bf86fe1 commit 684c4bf

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

clippy_lints/src/formatting.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
22
use clippy_utils::is_span_if;
33
use clippy_utils::source::snippet_opt;
4-
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
4+
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind};
55
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
66
use rustc_middle::lint::in_external_macro;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -144,7 +144,7 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
144144
let eq_span = lhs.span.between(rhs.span);
145145
if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind {
146146
if let Some(eq_snippet) = snippet_opt(cx, eq_span) {
147-
let op = UnOp::to_string(op);
147+
let op = op.as_str();
148148
let eqop_span = lhs.span.between(sub_rhs.span);
149149
if eq_snippet.ends_with('=') {
150150
span_lint_and_note(
@@ -177,11 +177,11 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
177177
&& let unop_operand_span = rhs.span.until(un_rhs.span)
178178
&& let Some(binop_snippet) = snippet_opt(cx, binop_span)
179179
&& let Some(unop_operand_snippet) = snippet_opt(cx, unop_operand_span)
180-
&& let binop_str = BinOpKind::to_string(&binop.node)
180+
&& let binop_str = binop.node.as_str()
181181
// no space after BinOp operator and space after UnOp operator
182182
&& binop_snippet.ends_with(binop_str) && unop_operand_snippet.ends_with(' ')
183183
{
184-
let unop_str = UnOp::to_string(op);
184+
let unop_str = op.as_str();
185185
let eqop_span = lhs.span.between(un_rhs.span);
186186
span_lint_and_help(
187187
cx,

clippy_lints/src/precedence.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl EarlyLintPass for Precedence {
7878
let sugg = format!(
7979
"({}) {} ({})",
8080
snippet_with_applicability(cx, left.span, "..", &mut applicability),
81-
op.to_string(),
81+
op.as_str(),
8282
snippet_with_applicability(cx, right.span, "..", &mut applicability)
8383
);
8484
span_sugg(expr, sugg, applicability);
@@ -87,7 +87,7 @@ impl EarlyLintPass for Precedence {
8787
let sugg = format!(
8888
"({}) {} {}",
8989
snippet_with_applicability(cx, left.span, "..", &mut applicability),
90-
op.to_string(),
90+
op.as_str(),
9191
snippet_with_applicability(cx, right.span, "..", &mut applicability)
9292
);
9393
span_sugg(expr, sugg, applicability);
@@ -96,7 +96,7 @@ impl EarlyLintPass for Precedence {
9696
let sugg = format!(
9797
"{} {} ({})",
9898
snippet_with_applicability(cx, left.span, "..", &mut applicability),
99-
op.to_string(),
99+
op.as_str(),
100100
snippet_with_applicability(cx, right.span, "..", &mut applicability)
101101
);
102102
span_sugg(expr, sugg, applicability);

clippy_lints/src/suspicious_operation_groupings.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ fn replace_left_sugg(
298298
) -> String {
299299
format!(
300300
"{left_suggestion} {} {}",
301-
binop.op.to_string(),
301+
binop.op.as_str(),
302302
snippet_with_applicability(cx, binop.right.span, "..", applicability),
303303
)
304304
}
@@ -312,7 +312,7 @@ fn replace_right_sugg(
312312
format!(
313313
"{} {} {right_suggestion}",
314314
snippet_with_applicability(cx, binop.left.span, "..", applicability),
315-
binop.op.to_string(),
315+
binop.op.as_str(),
316316
)
317317
}
318318

clippy_utils/src/sugg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ fn binop_to_string(op: AssocOp, lhs: &str, rhs: &str) -> String {
382382
| AssocOp::GreaterEqual => {
383383
format!(
384384
"{lhs} {} {rhs}",
385-
op.to_ast_binop().expect("Those are AST ops").to_string()
385+
op.to_ast_binop().expect("Those are AST ops").as_str()
386386
)
387387
},
388388
AssocOp::Assign => format!("{lhs} = {rhs}"),

0 commit comments

Comments
 (0)