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
1bcbb7c93b
commit
0efd2a9d8f
9 changed files with 20 additions and 20 deletions
|
@ -858,9 +858,9 @@ pub enum BinOpKind {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BinOpKind {
|
impl BinOpKind {
|
||||||
pub fn to_string(&self) -> &'static str {
|
pub fn as_str(&self) -> &'static str {
|
||||||
use BinOpKind::*;
|
use BinOpKind::*;
|
||||||
match *self {
|
match self {
|
||||||
Add => "+",
|
Add => "+",
|
||||||
Sub => "-",
|
Sub => "-",
|
||||||
Mul => "*",
|
Mul => "*",
|
||||||
|
@ -912,8 +912,8 @@ pub enum UnOp {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UnOp {
|
impl UnOp {
|
||||||
pub fn to_string(op: UnOp) -> &'static str {
|
pub fn as_str(&self) -> &'static str {
|
||||||
match op {
|
match self {
|
||||||
UnOp::Deref => "*",
|
UnOp::Deref => "*",
|
||||||
UnOp::Not => "!",
|
UnOp::Not => "!",
|
||||||
UnOp::Neg => "-",
|
UnOp::Neg => "-",
|
||||||
|
|
|
@ -255,12 +255,12 @@ impl<'a> State<'a> {
|
||||||
|
|
||||||
self.print_expr_maybe_paren(lhs, left_prec);
|
self.print_expr_maybe_paren(lhs, left_prec);
|
||||||
self.space();
|
self.space();
|
||||||
self.word_space(op.node.to_string());
|
self.word_space(op.node.as_str());
|
||||||
self.print_expr_maybe_paren(rhs, right_prec)
|
self.print_expr_maybe_paren(rhs, right_prec)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr) {
|
fn print_expr_unary(&mut self, op: ast::UnOp, expr: &ast::Expr) {
|
||||||
self.word(ast::UnOp::to_string(op));
|
self.word(op.as_str());
|
||||||
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
|
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -470,7 +470,7 @@ impl<'a> State<'a> {
|
||||||
let prec = AssocOp::Assign.precedence() as i8;
|
let prec = AssocOp::Assign.precedence() as i8;
|
||||||
self.print_expr_maybe_paren(lhs, prec + 1);
|
self.print_expr_maybe_paren(lhs, prec + 1);
|
||||||
self.space();
|
self.space();
|
||||||
self.word(op.node.to_string());
|
self.word(op.node.as_str());
|
||||||
self.word_space("=");
|
self.word_space("=");
|
||||||
self.print_expr_maybe_paren(rhs, prec);
|
self.print_expr_maybe_paren(rhs, prec);
|
||||||
}
|
}
|
||||||
|
|
|
@ -387,7 +387,7 @@ impl<'a> Parser<'a> {
|
||||||
if op.node.lazy() {
|
if op.node.lazy() {
|
||||||
self.sess.emit_err(errors::InvalidExpressionInLetElse {
|
self.sess.emit_err(errors::InvalidExpressionInLetElse {
|
||||||
span: init.span,
|
span: init.span,
|
||||||
operator: op.node.to_string(),
|
operator: op.node.as_str(),
|
||||||
sugg: errors::WrapExpressionInParentheses {
|
sugg: errors::WrapExpressionInParentheses {
|
||||||
left: init.span.shrink_to_lo(),
|
left: init.span.shrink_to_lo(),
|
||||||
right: init.span.shrink_to_hi(),
|
right: init.span.shrink_to_hi(),
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
|
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
|
||||||
use clippy_utils::is_span_if;
|
use clippy_utils::is_span_if;
|
||||||
use clippy_utils::source::snippet_opt;
|
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_lint::{EarlyContext, EarlyLintPass, LintContext};
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
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);
|
let eq_span = lhs.span.between(rhs.span);
|
||||||
if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind {
|
if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind {
|
||||||
if let Some(eq_snippet) = snippet_opt(cx, eq_span) {
|
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);
|
let eqop_span = lhs.span.between(sub_rhs.span);
|
||||||
if eq_snippet.ends_with('=') {
|
if eq_snippet.ends_with('=') {
|
||||||
span_lint_and_note(
|
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 unop_operand_span = rhs.span.until(un_rhs.span)
|
||||||
&& let Some(binop_snippet) = snippet_opt(cx, binop_span)
|
&& let Some(binop_snippet) = snippet_opt(cx, binop_span)
|
||||||
&& let Some(unop_operand_snippet) = snippet_opt(cx, unop_operand_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
|
// no space after BinOp operator and space after UnOp operator
|
||||||
&& binop_snippet.ends_with(binop_str) && unop_operand_snippet.ends_with(' ')
|
&& 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);
|
let eqop_span = lhs.span.between(un_rhs.span);
|
||||||
span_lint_and_help(
|
span_lint_and_help(
|
||||||
cx,
|
cx,
|
||||||
|
|
|
@ -78,7 +78,7 @@ impl EarlyLintPass for Precedence {
|
||||||
let sugg = format!(
|
let sugg = format!(
|
||||||
"({}) {} ({})",
|
"({}) {} ({})",
|
||||||
snippet_with_applicability(cx, left.span, "..", &mut applicability),
|
snippet_with_applicability(cx, left.span, "..", &mut applicability),
|
||||||
op.to_string(),
|
op.as_str(),
|
||||||
snippet_with_applicability(cx, right.span, "..", &mut applicability)
|
snippet_with_applicability(cx, right.span, "..", &mut applicability)
|
||||||
);
|
);
|
||||||
span_sugg(expr, sugg, applicability);
|
span_sugg(expr, sugg, applicability);
|
||||||
|
@ -87,7 +87,7 @@ impl EarlyLintPass for Precedence {
|
||||||
let sugg = format!(
|
let sugg = format!(
|
||||||
"({}) {} {}",
|
"({}) {} {}",
|
||||||
snippet_with_applicability(cx, left.span, "..", &mut applicability),
|
snippet_with_applicability(cx, left.span, "..", &mut applicability),
|
||||||
op.to_string(),
|
op.as_str(),
|
||||||
snippet_with_applicability(cx, right.span, "..", &mut applicability)
|
snippet_with_applicability(cx, right.span, "..", &mut applicability)
|
||||||
);
|
);
|
||||||
span_sugg(expr, sugg, applicability);
|
span_sugg(expr, sugg, applicability);
|
||||||
|
@ -96,7 +96,7 @@ impl EarlyLintPass for Precedence {
|
||||||
let sugg = format!(
|
let sugg = format!(
|
||||||
"{} {} ({})",
|
"{} {} ({})",
|
||||||
snippet_with_applicability(cx, left.span, "..", &mut applicability),
|
snippet_with_applicability(cx, left.span, "..", &mut applicability),
|
||||||
op.to_string(),
|
op.as_str(),
|
||||||
snippet_with_applicability(cx, right.span, "..", &mut applicability)
|
snippet_with_applicability(cx, right.span, "..", &mut applicability)
|
||||||
);
|
);
|
||||||
span_sugg(expr, sugg, applicability);
|
span_sugg(expr, sugg, applicability);
|
||||||
|
|
|
@ -298,7 +298,7 @@ fn replace_left_sugg(
|
||||||
) -> String {
|
) -> String {
|
||||||
format!(
|
format!(
|
||||||
"{left_suggestion} {} {}",
|
"{left_suggestion} {} {}",
|
||||||
binop.op.to_string(),
|
binop.op.as_str(),
|
||||||
snippet_with_applicability(cx, binop.right.span, "..", applicability),
|
snippet_with_applicability(cx, binop.right.span, "..", applicability),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -312,7 +312,7 @@ fn replace_right_sugg(
|
||||||
format!(
|
format!(
|
||||||
"{} {} {right_suggestion}",
|
"{} {} {right_suggestion}",
|
||||||
snippet_with_applicability(cx, binop.left.span, "..", applicability),
|
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 => {
|
| AssocOp::GreaterEqual => {
|
||||||
format!(
|
format!(
|
||||||
"{lhs} {} {rhs}",
|
"{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}"),
|
AssocOp::Assign => format!("{lhs} = {rhs}"),
|
||||||
|
|
|
@ -1933,7 +1933,7 @@ fn rewrite_unary_op(
|
||||||
shape: Shape,
|
shape: Shape,
|
||||||
) -> Option<String> {
|
) -> Option<String> {
|
||||||
// For some reason, an UnOp is not spanned like BinOp!
|
// For some reason, an UnOp is not spanned like BinOp!
|
||||||
rewrite_unary_prefix(context, ast::UnOp::to_string(op), expr, shape)
|
rewrite_unary_prefix(context, op.as_str(), expr, shape)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) enum RhsAssignKind<'ast> {
|
pub(crate) enum RhsAssignKind<'ast> {
|
||||||
|
|
|
@ -339,7 +339,7 @@ impl FlattenPair for ast::Expr {
|
||||||
if let Some(pop) = stack.pop() {
|
if let Some(pop) = stack.pop() {
|
||||||
match pop.kind {
|
match pop.kind {
|
||||||
ast::ExprKind::Binary(op, _, ref rhs) => {
|
ast::ExprKind::Binary(op, _, ref rhs) => {
|
||||||
separators.push(op.node.to_string());
|
separators.push(op.node.as_str());
|
||||||
node = rhs;
|
node = rhs;
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
|
|
Loading…
Add table
Reference in a new issue