Skip to content

Commit 1a86d80

Browse files
committed
Implement Sugg::hir_with_macro_callsite
1 parent 075c212 commit 1a86d80

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

clippy_lints/src/utils/sugg.rs

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Contains utility functions to generate suggestions.
22
#![deny(clippy::missing_docs_in_private_items)]
33

4-
use crate::utils::{higher, in_macro, snippet, snippet_opt};
4+
use crate::utils::{higher, in_macro, snippet, snippet_opt, snippet_with_macro_callsite};
55
use matches::matches;
66
use rustc::hir;
77
use rustc::lint::{EarlyContext, LateContext, LintContext};
@@ -46,38 +46,7 @@ impl<'a> Sugg<'a> {
4646
pub fn hir_opt(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<Self> {
4747
snippet_opt(cx, expr.span).map(|snippet| {
4848
let snippet = Cow::Owned(snippet);
49-
match expr.node {
50-
hir::ExprKind::AddrOf(..)
51-
| hir::ExprKind::Box(..)
52-
| hir::ExprKind::Closure(.., _)
53-
| hir::ExprKind::If(..)
54-
| hir::ExprKind::Unary(..)
55-
| hir::ExprKind::Match(..) => Sugg::MaybeParen(snippet),
56-
hir::ExprKind::Continue(..)
57-
| hir::ExprKind::Yield(..)
58-
| hir::ExprKind::Array(..)
59-
| hir::ExprKind::Block(..)
60-
| hir::ExprKind::Break(..)
61-
| hir::ExprKind::Call(..)
62-
| hir::ExprKind::Field(..)
63-
| hir::ExprKind::Index(..)
64-
| hir::ExprKind::InlineAsm(..)
65-
| hir::ExprKind::Lit(..)
66-
| hir::ExprKind::Loop(..)
67-
| hir::ExprKind::MethodCall(..)
68-
| hir::ExprKind::Path(..)
69-
| hir::ExprKind::Repeat(..)
70-
| hir::ExprKind::Ret(..)
71-
| hir::ExprKind::Struct(..)
72-
| hir::ExprKind::Tup(..)
73-
| hir::ExprKind::While(..)
74-
| hir::ExprKind::Err => Sugg::NonParen(snippet),
75-
hir::ExprKind::Assign(..) => Sugg::BinOp(AssocOp::Assign, snippet),
76-
hir::ExprKind::AssignOp(op, ..) => Sugg::BinOp(hirbinop2assignop(op), snippet),
77-
hir::ExprKind::Binary(op, ..) => Sugg::BinOp(AssocOp::from_ast_binop(higher::binop(op.node)), snippet),
78-
hir::ExprKind::Cast(..) => Sugg::BinOp(AssocOp::As, snippet),
79-
hir::ExprKind::Type(..) => Sugg::BinOp(AssocOp::Colon, snippet),
80-
}
49+
Self::hir_from_snippet(expr, snippet)
8150
})
8251
}
8352

@@ -111,6 +80,47 @@ impl<'a> Sugg<'a> {
11180
})
11281
}
11382

83+
pub fn hir_with_macro_callsite(cx: &LateContext<'_, '_>, expr: &hir::Expr, default: &'a str) -> Self {
84+
let snippet = snippet_with_macro_callsite(cx, expr.span, default);
85+
86+
Self::hir_from_snippet(expr, snippet)
87+
}
88+
89+
fn hir_from_snippet(expr: &hir::Expr, snippet: Cow<'a, str>) -> Self {
90+
match expr.node {
91+
hir::ExprKind::AddrOf(..)
92+
| hir::ExprKind::Box(..)
93+
| hir::ExprKind::Closure(.., _)
94+
| hir::ExprKind::If(..)
95+
| hir::ExprKind::Unary(..)
96+
| hir::ExprKind::Match(..) => Sugg::MaybeParen(snippet),
97+
hir::ExprKind::Continue(..)
98+
| hir::ExprKind::Yield(..)
99+
| hir::ExprKind::Array(..)
100+
| hir::ExprKind::Block(..)
101+
| hir::ExprKind::Break(..)
102+
| hir::ExprKind::Call(..)
103+
| hir::ExprKind::Field(..)
104+
| hir::ExprKind::Index(..)
105+
| hir::ExprKind::InlineAsm(..)
106+
| hir::ExprKind::Lit(..)
107+
| hir::ExprKind::Loop(..)
108+
| hir::ExprKind::MethodCall(..)
109+
| hir::ExprKind::Path(..)
110+
| hir::ExprKind::Repeat(..)
111+
| hir::ExprKind::Ret(..)
112+
| hir::ExprKind::Struct(..)
113+
| hir::ExprKind::Tup(..)
114+
| hir::ExprKind::While(..)
115+
| hir::ExprKind::Err => Sugg::NonParen(snippet),
116+
hir::ExprKind::Assign(..) => Sugg::BinOp(AssocOp::Assign, snippet),
117+
hir::ExprKind::AssignOp(op, ..) => Sugg::BinOp(hirbinop2assignop(op), snippet),
118+
hir::ExprKind::Binary(op, ..) => Sugg::BinOp(AssocOp::from_ast_binop(higher::binop(op.node)), snippet),
119+
hir::ExprKind::Cast(..) => Sugg::BinOp(AssocOp::As, snippet),
120+
hir::ExprKind::Type(..) => Sugg::BinOp(AssocOp::Colon, snippet),
121+
}
122+
}
123+
114124
/// Prepare a suggestion from an expression.
115125
pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self {
116126
use syntax::ast::RangeLimits;

0 commit comments

Comments
 (0)