1
1
use clippy_utils:: diagnostics:: { span_lint_and_help, span_lint_and_note} ;
2
2
use clippy_utils:: is_span_if;
3
- use clippy_utils:: source:: snippet_opt ;
3
+ use clippy_utils:: source:: SpanRangeExt ;
4
4
use rustc_ast:: ast:: { BinOpKind , Block , Expr , ExprKind , StmtKind } ;
5
5
use rustc_lint:: { EarlyContext , EarlyLintPass , LintContext } ;
6
6
use rustc_session:: declare_lint_pass;
@@ -170,23 +170,21 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
170
170
{
171
171
let eq_span = lhs. span . between ( rhs. span ) ;
172
172
if let ExprKind :: Unary ( op, ref sub_rhs) = rhs. kind
173
- && let Some ( eq_snippet ) = snippet_opt ( cx, eq_span )
173
+ && eq_span . check_source_text ( cx, |eq_snippet| eq_snippet . ends_with ( '=' ) )
174
174
{
175
175
let op = op. as_str ( ) ;
176
176
let eqop_span = lhs. span . between ( sub_rhs. span ) ;
177
- if eq_snippet. ends_with ( '=' ) {
178
- span_lint_and_note (
179
- cx,
180
- SUSPICIOUS_ASSIGNMENT_FORMATTING ,
181
- eqop_span,
182
- format ! (
183
- "this looks like you are trying to use `.. {op}= ..`, but you \
177
+ span_lint_and_note (
178
+ cx,
179
+ SUSPICIOUS_ASSIGNMENT_FORMATTING ,
180
+ eqop_span,
181
+ format ! (
182
+ "this looks like you are trying to use `.. {op}= ..`, but you \
184
183
really are doing `.. = ({op} ..)`"
185
- ) ,
186
- None ,
187
- format ! ( "to remove this lint, use either `{op}=` or `= {op}`" ) ,
188
- ) ;
189
- }
184
+ ) ,
185
+ None ,
186
+ format ! ( "to remove this lint, use either `{op}=` or `= {op}`" ) ,
187
+ ) ;
190
188
}
191
189
}
192
190
}
@@ -201,11 +199,11 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
201
199
&& let ExprKind :: Unary ( op, ref un_rhs) = rhs. kind
202
200
// from UnOp operator to UnOp operand
203
201
&& let unop_operand_span = rhs. span . until ( un_rhs. span )
204
- && let Some ( binop_snippet) = snippet_opt ( cx, binop_span)
205
- && let Some ( unop_operand_snippet) = snippet_opt ( cx, unop_operand_span)
206
202
&& let binop_str = binop. node . as_str ( )
207
- // no space after BinOp operator and space after UnOp operator
208
- && binop_snippet. ends_with ( binop_str) && unop_operand_snippet. ends_with ( ' ' )
203
+ // no space after BinOp operator
204
+ && binop_span. check_source_text ( cx, |binop_snippet| binop_snippet. ends_with ( binop_str) )
205
+ // ... and space after UnOp operator
206
+ && unop_operand_span. check_source_text ( cx, |unop_operand_snippet| unop_operand_snippet. ends_with ( ' ' ) )
209
207
{
210
208
let unop_str = op. as_str ( ) ;
211
209
let eqop_span = lhs. span . between ( un_rhs. span ) ;
@@ -239,7 +237,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
239
237
240
238
// the snippet should look like " else \n " with maybe comments anywhere
241
239
// it’s bad when there is a ‘\n’ after the “else”
242
- && let Some ( else_snippet) = snippet_opt ( cx, else_span )
240
+ && let Some ( else_snippet) = else_span . get_source_text ( cx)
243
241
&& let Some ( ( pre_else, post_else) ) = else_snippet. split_once ( "else" )
244
242
&& !else_snippet. contains ( '/' )
245
243
&& let Some ( ( _, post_else_post_eol) ) = post_else. split_once ( '\n' )
@@ -293,8 +291,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
293
291
&& has_unary_equivalent ( op. node )
294
292
&& lhs. span . eq_ctxt ( op. span )
295
293
&& let space_span = lhs. span . between ( op. span )
296
- && let Some ( space_snippet) = snippet_opt ( cx, space_span)
297
- && space_snippet. contains ( '\n' )
294
+ && space_span. check_source_text ( cx, |space_snippet| space_snippet. contains ( '\n' ) )
298
295
&& indentation ( cx, op. span ) <= indentation ( cx, lhs. span )
299
296
{
300
297
let lint_span = lhs. span . shrink_to_hi ( ) ;
@@ -322,8 +319,9 @@ fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
322
319
// If there is a line break between the two expressions, don't lint.
323
320
// If there is a non-whitespace character, this span came from a proc-macro.
324
321
&& let else_span = first. span . between ( second. span )
325
- && let Some ( else_snippet) = snippet_opt ( cx, else_span)
326
- && !else_snippet. chars ( ) . any ( |c| c == '\n' || !c. is_whitespace ( ) )
322
+ && else_span. check_source_text ( cx, |else_snippet| {
323
+ !else_snippet. chars ( ) . any ( |c| c == '\n' || !c. is_whitespace ( ) )
324
+ } )
327
325
{
328
326
let ( looks_like, next_thing) = if is_if ( second) {
329
327
( "an `else if`" , "the second `if`" )
0 commit comments