11use clippy_utils:: diagnostics:: { span_lint_and_help, span_lint_and_note} ;
22use clippy_utils:: is_span_if;
3- use clippy_utils:: source:: snippet_opt ;
3+ use clippy_utils:: source:: SpanRangeExt ;
44use rustc_ast:: ast:: { BinOpKind , Block , Expr , ExprKind , StmtKind } ;
55use rustc_lint:: { EarlyContext , EarlyLintPass , LintContext } ;
66use rustc_session:: declare_lint_pass;
@@ -170,23 +170,21 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
170170 {
171171 let eq_span = lhs. span . between ( rhs. span ) ;
172172 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 ( '=' ) )
174174 {
175175 let op = op. as_str ( ) ;
176176 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 \
184183 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+ ) ;
190188 }
191189 }
192190}
@@ -201,11 +199,11 @@ fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
201199 && let ExprKind :: Unary ( op, ref un_rhs) = rhs. kind
202200 // from UnOp operator to UnOp operand
203201 && 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)
206202 && 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 ( ' ' ) )
209207 {
210208 let unop_str = op. as_str ( ) ;
211209 let eqop_span = lhs. span . between ( un_rhs. span ) ;
@@ -239,7 +237,7 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
239237
240238 // the snippet should look like " else \n " with maybe comments anywhere
241239 // 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)
243241 && let Some ( ( pre_else, post_else) ) = else_snippet. split_once ( "else" )
244242 && !else_snippet. contains ( '/' )
245243 && let Some ( ( _, post_else_post_eol) ) = post_else. split_once ( '\n' )
@@ -293,8 +291,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
293291 && has_unary_equivalent ( op. node )
294292 && lhs. span . eq_ctxt ( op. span )
295293 && 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' ) )
298295 && indentation ( cx, op. span ) <= indentation ( cx, lhs. span )
299296 {
300297 let lint_span = lhs. span . shrink_to_hi ( ) ;
@@ -322,8 +319,9 @@ fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
322319 // If there is a line break between the two expressions, don't lint.
323320 // If there is a non-whitespace character, this span came from a proc-macro.
324321 && 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+ } )
327325 {
328326 let ( looks_like, next_thing) = if is_if ( second) {
329327 ( "an `else if`" , "the second `if`" )
0 commit comments