11use crate :: methods:: MAP_WITH_UNUSED_ARGUMENT_OVER_RANGES ;
22use clippy_utils:: diagnostics:: span_lint_and_then;
33use clippy_utils:: msrvs:: { self , Msrv } ;
4- use clippy_utils:: source:: snippet_with_applicability ;
4+ use clippy_utils:: source:: snippet_with_context ;
55use clippy_utils:: sugg:: Sugg ;
66use clippy_utils:: { eager_or_lazy, higher, std_or_core, usage} ;
77use rustc_ast:: LitKind ;
@@ -10,12 +10,13 @@ use rustc_data_structures::packed::Pu128;
1010use rustc_errors:: Applicability ;
1111use rustc_hir:: { Body , Closure , Expr , ExprKind } ;
1212use rustc_lint:: LateContext ;
13- use rustc_span:: Span ;
13+ use rustc_span:: { Span , SyntaxContext } ;
1414
1515fn extract_count_with_applicability (
1616 cx : & LateContext < ' _ > ,
1717 range : higher:: Range < ' _ > ,
1818 applicability : & mut Applicability ,
19+ ctxt : SyntaxContext ,
1920) -> Option < String > {
2021 let start = range. start ?;
2122 let end = range. end ?;
@@ -40,7 +41,7 @@ fn extract_count_with_applicability(
4041 } ;
4142 return Some ( format ! ( "{count}" ) ) ;
4243 }
43- let end_snippet = Sugg :: hir_with_applicability ( cx, end, "..." , applicability)
44+ let end_snippet = Sugg :: hir_with_context ( cx, end, ctxt , "..." , applicability)
4445 . maybe_paren ( )
4546 . into_string ( ) ;
4647 if lower_bound == 0 {
@@ -74,52 +75,43 @@ pub(super) fn check(
7475 value : body_expr,
7576 } = body_hir
7677 && !usage:: BindingUsageFinder :: are_params_used ( cx, body_hir)
77- && let Some ( count) = extract_count_with_applicability ( cx, range, & mut applicability)
78+ && let ctxt = ex. span . ctxt ( )
79+ && let Some ( count) = extract_count_with_applicability ( cx, range, & mut applicability, ctxt)
7880 && let Some ( exec_context) = std_or_core ( cx)
7981 {
80- let method_to_use_name;
81- let new_span;
82- let use_take;
83-
84- if eager_or_lazy:: switch_to_eager_eval ( cx, body_expr) {
82+ let ( method_to_use_name, new_span, use_take) = if eager_or_lazy:: switch_to_eager_eval ( cx, body_expr) {
8583 if msrv. meets ( cx, msrvs:: REPEAT_N ) {
86- method_to_use_name = "repeat_n" ;
87- let body_snippet = snippet_with_applicability ( cx, body_expr. span , ".." , & mut applicability) ;
88- new_span = ( arg. span , format ! ( "{body_snippet}, {count}" ) ) ;
89- use_take = false ;
84+ let ( body_snippet, _) = snippet_with_context ( cx, body_expr. span , ctxt, ".." , & mut applicability) ;
85+ ( "repeat_n" , ( arg. span , format ! ( "{body_snippet}, {count}" ) ) , false )
9086 } else {
91- method_to_use_name = "repeat" ;
92- let body_snippet = snippet_with_applicability ( cx, body_expr. span , ".." , & mut applicability) ;
93- new_span = ( arg. span , body_snippet. to_string ( ) ) ;
94- use_take = true ;
87+ let ( body_snippet, _) = snippet_with_context ( cx, body_expr. span , ctxt, ".." , & mut applicability) ;
88+ ( "repeat" , ( arg. span , body_snippet. to_string ( ) ) , true )
9589 }
9690 } else if msrv. meets ( cx, msrvs:: REPEAT_WITH ) {
97- method_to_use_name = "repeat_with" ;
98- new_span = ( param. span , String :: new ( ) ) ;
99- use_take = true ;
91+ ( "repeat_with" , ( param. span , String :: new ( ) ) , true )
10092 } else {
10193 return ;
102- }
103-
104- // We need to provide nonempty parts to diag.multipart_suggestion so we
105- // collate all our parts here and then remove those that are empty.
106- let mut parts = vec ! [
107- (
108- ex. span. with_hi( method_name_span. hi( ) ) ,
109- format!( "{exec_context}::iter::{method_to_use_name}" ) ,
110- ) ,
111- new_span,
112- ] ;
113- if use_take {
114- parts. push ( ( ex. span . shrink_to_hi ( ) , format ! ( ".take({count})" ) ) ) ;
115- }
94+ } ;
11695
11796 span_lint_and_then (
11897 cx,
11998 MAP_WITH_UNUSED_ARGUMENT_OVER_RANGES ,
12099 ex. span ,
121100 "map of a closure that does not depend on its parameter over a range" ,
122101 |diag| {
102+ // We need to provide nonempty parts to diag.multipart_suggestion so we
103+ // collate all our parts here and then remove those that are empty.
104+ let mut parts = vec ! [
105+ (
106+ ex. span. with_hi( method_name_span. hi( ) ) ,
107+ format!( "{exec_context}::iter::{method_to_use_name}" ) ,
108+ ) ,
109+ new_span,
110+ ] ;
111+ if use_take {
112+ parts. push ( ( ex. span . shrink_to_hi ( ) , format ! ( ".take({count})" ) ) ) ;
113+ }
114+
123115 diag. multipart_suggestion (
124116 if use_take {
125117 format ! ( "remove the explicit range and use `{method_to_use_name}` and `take`" )
0 commit comments