Skip to content

Commit

Permalink
Rework ast::BinOpKind::to_string and ast::UnOp::to_string.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
nnethercote committed Nov 27, 2023
1 parent bf86fe1 commit 684c4bf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions clippy_lints/src/formatting.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/precedence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/suspicious_operation_groupings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
}
Expand All @@ -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(),
)
}

Expand Down
2 changes: 1 addition & 1 deletion clippy_utils/src/sugg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"),
Expand Down

0 comments on commit 684c4bf

Please sign in to comment.