X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Ftools%2Fclippy%2Fclippy_utils%2Fsrc%2Fsource.rs;h=cd5dcfdaca34b64245632331bd7707c36205adcd;hb=cce9e72c55994335f8d1dac892cca755b65c8f43;hp=eacfa91ba556d41d454f2b3a63c5a325ae4311cd;hpb=f8a2e491ebec34113563cf8683f6fec2646cebb1;p=rust.git diff --git a/src/tools/clippy/clippy_utils/src/source.rs b/src/tools/clippy/clippy_utils/src/source.rs index eacfa91ba55..cd5dcfdaca3 100644 --- a/src/tools/clippy/clippy_utils/src/source.rs +++ b/src/tools/clippy/clippy_utils/src/source.rs @@ -5,6 +5,7 @@ use rustc_errors::Applicability; use rustc_hir::{Expr, ExprKind}; use rustc_lint::{LateContext, LintContext}; +use rustc_session::Session; use rustc_span::hygiene; use rustc_span::source_map::{original_sp, SourceMap}; use rustc_span::{BytePos, Pos, Span, SpanData, SyntaxContext, DUMMY_SP}; @@ -204,11 +205,20 @@ pub fn snippet_with_applicability<'a, T: LintContext>( span: Span, default: &'a str, applicability: &mut Applicability, +) -> Cow<'a, str> { + snippet_with_applicability_sess(cx.sess(), span, default, applicability) +} + +fn snippet_with_applicability_sess<'a>( + sess: &Session, + span: Span, + default: &'a str, + applicability: &mut Applicability, ) -> Cow<'a, str> { if *applicability != Applicability::Unspecified && span.from_expansion() { *applicability = Applicability::MaybeIncorrect; } - snippet_opt(cx, span).map_or_else( + snippet_opt_sess(sess, span).map_or_else( || { if *applicability == Applicability::MachineApplicable { *applicability = Applicability::HasPlaceholders; @@ -226,8 +236,12 @@ pub fn snippet_with_macro_callsite<'a, T: LintContext>(cx: &T, span: Span, defau } /// Converts a span to a code snippet. Returns `None` if not available. -pub fn snippet_opt(cx: &T, span: Span) -> Option { - cx.sess().source_map().span_to_snippet(span).ok() +pub fn snippet_opt(cx: &impl LintContext, span: Span) -> Option { + snippet_opt_sess(cx.sess(), span) +} + +fn snippet_opt_sess(sess: &Session, span: Span) -> Option { + sess.source_map().span_to_snippet(span).ok() } /// Converts a span (from a block) to a code snippet if available, otherwise use default. @@ -277,8 +291,8 @@ pub fn snippet_block<'a, T: LintContext>( /// Same as `snippet_block`, but adapts the applicability level by the rules of /// `snippet_with_applicability`. -pub fn snippet_block_with_applicability<'a, T: LintContext>( - cx: &T, +pub fn snippet_block_with_applicability<'a>( + cx: &impl LintContext, span: Span, default: &'a str, indent_relative_to: Option, @@ -299,7 +313,17 @@ pub fn snippet_block_with_applicability<'a, T: LintContext>( /// /// This will also return whether or not the snippet is a macro call. pub fn snippet_with_context<'a>( - cx: &LateContext<'_>, + cx: &impl LintContext, + span: Span, + outer: SyntaxContext, + default: &'a str, + applicability: &mut Applicability, +) -> (Cow<'a, str>, bool) { + snippet_with_context_sess(cx.sess(), span, outer, default, applicability) +} + +fn snippet_with_context_sess<'a>( + sess: &Session, span: Span, outer: SyntaxContext, default: &'a str, @@ -318,7 +342,7 @@ pub fn snippet_with_context<'a>( ); ( - snippet_with_applicability(cx, span, default, applicability), + snippet_with_applicability_sess(sess, span, default, applicability), is_macro_call, ) }