|
1 | 1 | //! Contains utility functions to generate suggestions.
|
2 | 2 | #![deny(clippy::missing_docs_in_private_items)]
|
3 | 3 |
|
4 |
| -use crate::utils::{higher, in_macro, snippet, snippet_opt}; |
| 4 | +use crate::utils::{higher, in_macro, snippet, snippet_opt, snippet_with_macro_callsite}; |
5 | 5 | use matches::matches;
|
6 | 6 | use rustc::hir;
|
7 | 7 | use rustc::lint::{EarlyContext, LateContext, LintContext};
|
@@ -46,38 +46,7 @@ impl<'a> Sugg<'a> {
|
46 | 46 | pub fn hir_opt(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<Self> {
|
47 | 47 | snippet_opt(cx, expr.span).map(|snippet| {
|
48 | 48 | 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) |
81 | 50 | })
|
82 | 51 | }
|
83 | 52 |
|
@@ -111,6 +80,50 @@ impl<'a> Sugg<'a> {
|
111 | 80 | })
|
112 | 81 | }
|
113 | 82 |
|
| 83 | + /// Same as `hir`, but will use the pre expansion span if the `expr` was in a macro. |
| 84 | + pub fn hir_with_macro_callsite(cx: &LateContext<'_, '_>, expr: &hir::Expr, default: &'a str) -> Self { |
| 85 | + let snippet = snippet_with_macro_callsite(cx, expr.span, default); |
| 86 | + |
| 87 | + Self::hir_from_snippet(expr, snippet) |
| 88 | + } |
| 89 | + |
| 90 | + /// Generate a suggestion for an expression with the given snippet. This is used by the `hir_*` |
| 91 | + /// function variants of `Sugg`, since these use different snippet functions. |
| 92 | + fn hir_from_snippet(expr: &hir::Expr, snippet: Cow<'a, str>) -> Self { |
| 93 | + match expr.node { |
| 94 | + hir::ExprKind::AddrOf(..) |
| 95 | + | hir::ExprKind::Box(..) |
| 96 | + | hir::ExprKind::Closure(.., _) |
| 97 | + | hir::ExprKind::If(..) |
| 98 | + | hir::ExprKind::Unary(..) |
| 99 | + | hir::ExprKind::Match(..) => Sugg::MaybeParen(snippet), |
| 100 | + hir::ExprKind::Continue(..) |
| 101 | + | hir::ExprKind::Yield(..) |
| 102 | + | hir::ExprKind::Array(..) |
| 103 | + | hir::ExprKind::Block(..) |
| 104 | + | hir::ExprKind::Break(..) |
| 105 | + | hir::ExprKind::Call(..) |
| 106 | + | hir::ExprKind::Field(..) |
| 107 | + | hir::ExprKind::Index(..) |
| 108 | + | hir::ExprKind::InlineAsm(..) |
| 109 | + | hir::ExprKind::Lit(..) |
| 110 | + | hir::ExprKind::Loop(..) |
| 111 | + | hir::ExprKind::MethodCall(..) |
| 112 | + | hir::ExprKind::Path(..) |
| 113 | + | hir::ExprKind::Repeat(..) |
| 114 | + | hir::ExprKind::Ret(..) |
| 115 | + | hir::ExprKind::Struct(..) |
| 116 | + | hir::ExprKind::Tup(..) |
| 117 | + | hir::ExprKind::While(..) |
| 118 | + | hir::ExprKind::Err => Sugg::NonParen(snippet), |
| 119 | + hir::ExprKind::Assign(..) => Sugg::BinOp(AssocOp::Assign, snippet), |
| 120 | + hir::ExprKind::AssignOp(op, ..) => Sugg::BinOp(hirbinop2assignop(op), snippet), |
| 121 | + hir::ExprKind::Binary(op, ..) => Sugg::BinOp(AssocOp::from_ast_binop(higher::binop(op.node)), snippet), |
| 122 | + hir::ExprKind::Cast(..) => Sugg::BinOp(AssocOp::As, snippet), |
| 123 | + hir::ExprKind::Type(..) => Sugg::BinOp(AssocOp::Colon, snippet), |
| 124 | + } |
| 125 | + } |
| 126 | + |
114 | 127 | /// Prepare a suggestion from an expression.
|
115 | 128 | pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self {
|
116 | 129 | use syntax::ast::RangeLimits;
|
|
0 commit comments