Skip to content

Commit 0efd2a9

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 1bcbb7c commit 0efd2a9

File tree

9 files changed

+20
-20
lines changed

9 files changed

+20
-20
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -858,9 +858,9 @@ pub enum BinOpKind {
858858
}
859859

860860
impl BinOpKind {
861-
pub fn to_string(&self) -> &'static str {
861+
pub fn as_str(&self) -> &'static str {
862862
use BinOpKind::*;
863-
match *self {
863+
match self {
864864
Add => "+",
865865
Sub => "-",
866866
Mul => "*",
@@ -912,8 +912,8 @@ pub enum UnOp {
912912
}
913913

914914
impl UnOp {
915-
pub fn to_string(op: UnOp) -> &'static str {
916-
match op {
915+
pub fn as_str(&self) -> &'static str {
916+
match self {
917917
UnOp::Deref => "*",
918918
UnOp::Not => "!",
919919
UnOp::Neg => "-",

Diff for: compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,12 @@ impl<'a> State<'a> {
255255

256256
self.print_expr_maybe_paren(lhs, left_prec);
257257
self.space();
258-
self.word_space(op.node.to_string());
258+
self.word_space(op.node.as_str());
259259
self.print_expr_maybe_paren(rhs, right_prec)
260260
}
261261

262262
fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr) {
263-
self.word(ast::UnOp::to_string(op));
263+
self.word(op.as_str());
264264
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
265265
}
266266

@@ -470,7 +470,7 @@ impl<'a> State<'a> {
470470
let prec = AssocOp::Assign.precedence() as i8;
471471
self.print_expr_maybe_paren(lhs, prec + 1);
472472
self.space();
473-
self.word(op.node.to_string());
473+
self.word(op.node.as_str());
474474
self.word_space("=");
475475
self.print_expr_maybe_paren(rhs, prec);
476476
}

Diff for: compiler/rustc_parse/src/parser/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl<'a> Parser<'a> {
387387
if op.node.lazy() {
388388
self.sess.emit_err(errors::InvalidExpressionInLetElse {
389389
span: init.span,
390-
operator: op.node.to_string(),
390+
operator: op.node.as_str(),
391391
sugg: errors::WrapExpressionInParentheses {
392392
left: init.span.shrink_to_lo(),
393393
right: init.span.shrink_to_hi(),

Diff for: src/tools/clippy/clippy_lints/src/formatting.rs

+4-4
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,

Diff for: src/tools/clippy/clippy_lints/src/precedence.rs

+3-3
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);

Diff for: src/tools/clippy/clippy_lints/src/suspicious_operation_groupings.rs

+2-2
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

Diff for: src/tools/clippy/clippy_utils/src/sugg.rs

+1-1
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}"),

Diff for: src/tools/rustfmt/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,7 @@ fn rewrite_unary_op(
19331933
shape: Shape,
19341934
) -> Option<String> {
19351935
// For some reason, an UnOp is not spanned like BinOp!
1936-
rewrite_unary_prefix(context, ast::UnOp::to_string(op), expr, shape)
1936+
rewrite_unary_prefix(context, op.as_str(), expr, shape)
19371937
}
19381938

19391939
pub(crate) enum RhsAssignKind<'ast> {

Diff for: src/tools/rustfmt/src/pairs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl FlattenPair for ast::Expr {
339339
if let Some(pop) = stack.pop() {
340340
match pop.kind {
341341
ast::ExprKind::Binary(op, _, ref rhs) => {
342-
separators.push(op.node.to_string());
342+
separators.push(op.node.as_str());
343343
node = rhs;
344344
}
345345
_ => unreachable!(),

0 commit comments

Comments
 (0)