Skip to content

Commit cc18556

Browse files
committed
Use utils::sugg in swap lints
1 parent 139b977 commit cc18556

File tree

2 files changed

+54
-43
lines changed

2 files changed

+54
-43
lines changed

clippy_lints/src/swap.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use rustc::hir::*;
22
use rustc::lint::*;
33
use rustc::ty;
44
use syntax::codemap::mk_sp;
5-
use utils::{differing_macro_contexts, match_type, paths, snippet, snippet_opt, span_lint_and_then, walk_ptrs_ty, SpanlessEq};
5+
use utils::{differing_macro_contexts, match_type, paths, snippet, span_lint_and_then, walk_ptrs_ty, SpanlessEq};
6+
use utils::sugg::Sugg;
67

78
/// **What it does:** This lints manual swapping.
89
///
@@ -100,17 +101,17 @@ fn check_manual_swap(cx: &LateContext, block: &Block) {
100101
}
101102

102103
let (replace, what, sugg) = if let Some((slice, idx1, idx2)) = check_for_slice(cx, lhs1, lhs2) {
103-
if let Some(slice) = snippet_opt(cx, slice.span) {
104+
if let Some(slice) = Sugg::hir_opt(cx, slice) {
104105
(false,
105106
format!(" elements of `{}`", slice),
106-
format!("{}.swap({}, {})",slice, snippet(cx, idx1.span, ".."), snippet(cx, idx2.span, "..")))
107+
format!("{}.swap({}, {})", slice.maybe_par(), snippet(cx, idx1.span, ".."), snippet(cx, idx2.span, "..")))
107108
} else {
108109
(false, "".to_owned(), "".to_owned())
109110
}
110111
} else {
111-
if let (Some(first), Some(second)) = (snippet_opt(cx, lhs1.span), snippet_opt(cx, rhs1.span)) {
112+
if let (Some(first), Some(second)) = (Sugg::hir_opt(cx, lhs1), Sugg::hir_opt(cx, rhs1)) {
112113
(true, format!(" `{}` and `{}`", first, second),
113-
format!("std::mem::swap(&mut {}, &mut {})", first, second))
114+
format!("std::mem::swap({}, {})", first.mut_addr(), second.mut_addr()))
114115
} else {
115116
(true, "".to_owned(), "".to_owned())
116117
}
@@ -147,8 +148,8 @@ fn check_suspicious_swap(cx: &LateContext, block: &Block) {
147148
SpanlessEq::new(cx).ignore_fn().eq_expr(lhs0, rhs1),
148149
SpanlessEq::new(cx).ignore_fn().eq_expr(lhs1, rhs0)
149150
], {
150-
let (what, lhs, rhs) = if let (Some(first), Some(second)) = (snippet_opt(cx, lhs0.span), snippet_opt(cx, rhs0.span)) {
151-
(format!(" `{}` and `{}`", first, second), first, second)
151+
let (what, lhs, rhs) = if let (Some(first), Some(second)) = (Sugg::hir_opt(cx, lhs0), Sugg::hir_opt(cx, rhs0)) {
152+
(format!(" `{}` and `{}`", first, second), first.mut_addr().to_string(), second.mut_addr().to_string())
152153
} else {
153154
("".to_owned(), "".to_owned(), "".to_owned())
154155
};
@@ -162,7 +163,7 @@ fn check_suspicious_swap(cx: &LateContext, block: &Block) {
162163
|db| {
163164
if !what.is_empty() {
164165
db.span_suggestion(span, "try",
165-
format!("std::mem::swap(&mut {}, &mut {})", lhs, rhs));
166+
format!("std::mem::swap({}, {})", lhs, rhs));
166167
db.note("or maybe you should use `std::mem::replace`?");
167168
}
168169
});

clippy_lints/src/utils/sugg.rs

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::borrow::Cow;
44
use std;
55
use syntax::ast;
66
use syntax::util::parser::AssocOp;
7-
use utils::{higher, snippet};
7+
use utils::{higher, snippet, snippet_opt};
88
use syntax::print::pprust::binop_to_string;
99

1010
/// A helper type to build suggestion correctly handling parenthesis.
@@ -31,43 +31,48 @@ impl<'a> std::fmt::Display for Sugg<'a> {
3131
}
3232

3333
impl<'a> Sugg<'a> {
34-
pub fn hir(cx: &LateContext, expr: &'a hir::Expr, default: &'a str) -> Sugg<'a> {
35-
let snippet = snippet(cx, expr.span, default);
34+
pub fn hir_opt(cx: &LateContext, expr: &hir::Expr) -> Option<Sugg<'a>> {
35+
snippet_opt(cx, expr.span).map(|snippet| {
36+
let snippet = Cow::Owned(snippet);
37+
match expr.node {
38+
hir::ExprAddrOf(..) |
39+
hir::ExprBox(..) |
40+
hir::ExprClosure(..) |
41+
hir::ExprIf(..) |
42+
hir::ExprUnary(..) |
43+
hir::ExprMatch(..) => Sugg::MaybeParen(snippet),
44+
hir::ExprAgain(..) |
45+
hir::ExprBlock(..) |
46+
hir::ExprBreak(..) |
47+
hir::ExprCall(..) |
48+
hir::ExprField(..) |
49+
hir::ExprIndex(..) |
50+
hir::ExprInlineAsm(..) |
51+
hir::ExprLit(..) |
52+
hir::ExprLoop(..) |
53+
hir::ExprMethodCall(..) |
54+
hir::ExprPath(..) |
55+
hir::ExprRepeat(..) |
56+
hir::ExprRet(..) |
57+
hir::ExprStruct(..) |
58+
hir::ExprTup(..) |
59+
hir::ExprTupField(..) |
60+
hir::ExprVec(..) |
61+
hir::ExprWhile(..) => Sugg::NonParen(snippet),
62+
hir::ExprAssign(..) => Sugg::BinOp(AssocOp::Assign, snippet),
63+
hir::ExprAssignOp(op, ..) => Sugg::BinOp(hirbinop2assignop(op), snippet),
64+
hir::ExprBinary(op, ..) => Sugg::BinOp(AssocOp::from_ast_binop(higher::binop(op.node)), snippet),
65+
hir::ExprCast(..) => Sugg::BinOp(AssocOp::As, snippet),
66+
hir::ExprType(..) => Sugg::BinOp(AssocOp::Colon, snippet),
67+
}
68+
})
69+
}
3670

37-
match expr.node {
38-
hir::ExprAddrOf(..) |
39-
hir::ExprBox(..) |
40-
hir::ExprClosure(..) |
41-
hir::ExprIf(..) |
42-
hir::ExprUnary(..) |
43-
hir::ExprMatch(..) => Sugg::MaybeParen(snippet),
44-
hir::ExprAgain(..) |
45-
hir::ExprBlock(..) |
46-
hir::ExprBreak(..) |
47-
hir::ExprCall(..) |
48-
hir::ExprField(..) |
49-
hir::ExprIndex(..) |
50-
hir::ExprInlineAsm(..) |
51-
hir::ExprLit(..) |
52-
hir::ExprLoop(..) |
53-
hir::ExprMethodCall(..) |
54-
hir::ExprPath(..) |
55-
hir::ExprRepeat(..) |
56-
hir::ExprRet(..) |
57-
hir::ExprStruct(..) |
58-
hir::ExprTup(..) |
59-
hir::ExprTupField(..) |
60-
hir::ExprVec(..) |
61-
hir::ExprWhile(..) => Sugg::NonParen(snippet),
62-
hir::ExprAssign(..) => Sugg::BinOp(AssocOp::Assign, snippet),
63-
hir::ExprAssignOp(op, ..) => Sugg::BinOp(hirbinop2assignop(op), snippet),
64-
hir::ExprBinary(op, ..) => Sugg::BinOp(AssocOp::from_ast_binop(higher::binop(op.node)), snippet),
65-
hir::ExprCast(..) => Sugg::BinOp(AssocOp::As, snippet),
66-
hir::ExprType(..) => Sugg::BinOp(AssocOp::Colon, snippet),
67-
}
71+
pub fn hir(cx: &LateContext, expr: &hir::Expr, default: &'a str) -> Sugg<'a> {
72+
Self::hir_opt(cx, expr).unwrap_or_else(|| Sugg::NonParen(Cow::Borrowed(default)))
6873
}
6974

70-
pub fn ast(cx: &EarlyContext, expr: &'a ast::Expr, default: &'a str) -> Sugg<'a> {
75+
pub fn ast(cx: &EarlyContext, expr: &ast::Expr, default: &'a str) -> Sugg<'a> {
7176
use syntax::ast::RangeLimits;
7277

7378
let snippet = snippet(cx, expr.span, default);
@@ -124,6 +129,11 @@ impl<'a> Sugg<'a> {
124129
make_unop("&", self)
125130
}
126131

132+
/// Convenience method to create the `&mut <expr>` suggestion.
133+
pub fn mut_addr(self) -> Sugg<'static> {
134+
make_unop("&mut ", self)
135+
}
136+
127137
/// Convenience method to create the `<lhs>..<rhs>` or `<lhs>...<rhs>` suggestion.
128138
pub fn range(self, end: Self, limit: ast::RangeLimits) -> Sugg<'static> {
129139
match limit {

0 commit comments

Comments
 (0)