mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-05 03:38:29 +00:00
Auto merge of #7542 - LeSeulArtichaut:cleanups, r=flip1995
Cleanup usage of `span_to_snippet` and `LintContext::sess` - avoid using `SourceMap::span_to_snippet` directly and use `clippy_utils::source::snippet_opt` instead - don't use `LintContext::sess()` on `EarlyContext`s which have a `sess` field directly available, saving the import of `LintContext` changelog: none
This commit is contained in:
commit
14d2aa1582
@ -1,6 +1,6 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_help;
|
use clippy_utils::diagnostics::span_lint_and_help;
|
||||||
use rustc_ast::ast::{Expr, ExprKind};
|
use rustc_ast::ast::{Expr, ExprKind};
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ declare_lint_pass!(AsConversions => [AS_CONVERSIONS]);
|
|||||||
|
|
||||||
impl EarlyLintPass for AsConversions {
|
impl EarlyLintPass for AsConversions {
|
||||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||||
if in_external_macro(cx.sess(), expr.span) {
|
if in_external_macro(cx.sess, expr.span) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use clippy_utils::diagnostics::span_lint_and_help;
|
use clippy_utils::diagnostics::span_lint_and_help;
|
||||||
use rustc_ast::ast::{Expr, ExprKind};
|
use rustc_ast::ast::{Expr, ExprKind};
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ declare_lint_pass!(ElseIfWithoutElse => [ELSE_IF_WITHOUT_ELSE]);
|
|||||||
|
|
||||||
impl EarlyLintPass for ElseIfWithoutElse {
|
impl EarlyLintPass for ElseIfWithoutElse {
|
||||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, mut item: &Expr) {
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, mut item: &Expr) {
|
||||||
if in_external_macro(cx.sess(), item.span) {
|
if in_external_macro(cx.sess, item.span) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
use clippy_utils::diagnostics::span_lint_and_help;
|
use clippy_utils::diagnostics::span_lint_and_help;
|
||||||
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, UnOp};
|
use rustc_ast::ast::{BinOpKind, Expr, ExprKind, UnOp};
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ declare_lint_pass!(IfNotElse => [IF_NOT_ELSE]);
|
|||||||
|
|
||||||
impl EarlyLintPass for IfNotElse {
|
impl EarlyLintPass for IfNotElse {
|
||||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, item: &Expr) {
|
||||||
if in_external_macro(cx.sess(), item.span) {
|
if in_external_macro(cx.sess, item.span) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let ExprKind::If(ref cond, _, Some(ref els)) = item.kind {
|
if let ExprKind::If(ref cond, _, Some(ref els)) = item.kind {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use clippy_utils::diagnostics::span_lint;
|
use clippy_utils::diagnostics::span_lint;
|
||||||
use rustc_ast::ast::{Block, ItemKind, StmtKind};
|
use rustc_ast::ast::{Block, ItemKind, StmtKind};
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ declare_lint_pass!(ItemsAfterStatements => [ITEMS_AFTER_STATEMENTS]);
|
|||||||
|
|
||||||
impl EarlyLintPass for ItemsAfterStatements {
|
impl EarlyLintPass for ItemsAfterStatements {
|
||||||
fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
|
fn check_block(&mut self, cx: &EarlyContext<'_>, item: &Block) {
|
||||||
if in_external_macro(cx.sess(), item.span) {
|
if in_external_macro(cx.sess, item.span) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,7 +68,7 @@ impl EarlyLintPass for ItemsAfterStatements {
|
|||||||
// lint on all further items
|
// lint on all further items
|
||||||
for stmt in stmts {
|
for stmt in stmts {
|
||||||
if let StmtKind::Item(ref it) = *stmt {
|
if let StmtKind::Item(ref it) = *stmt {
|
||||||
if in_external_macro(cx.sess(), it.span) {
|
if in_external_macro(cx.sess, it.span) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if let ItemKind::MacroDef(..) = it.kind {
|
if let ItemKind::MacroDef(..) = it.kind {
|
||||||
|
@ -10,7 +10,7 @@ use clippy_utils::{
|
|||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_ast::ast::{Expr, ExprKind, Lit, LitKind};
|
use rustc_ast::ast::{Expr, ExprKind, Lit, LitKind};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||||
use std::iter;
|
use std::iter;
|
||||||
@ -222,7 +222,7 @@ impl_lint_pass!(LiteralDigitGrouping => [
|
|||||||
|
|
||||||
impl EarlyLintPass for LiteralDigitGrouping {
|
impl EarlyLintPass for LiteralDigitGrouping {
|
||||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||||
if in_external_macro(cx.sess(), expr.span) {
|
if in_external_macro(cx.sess, expr.span) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -415,7 +415,7 @@ impl_lint_pass!(DecimalLiteralRepresentation => [DECIMAL_LITERAL_REPRESENTATION]
|
|||||||
|
|
||||||
impl EarlyLintPass for DecimalLiteralRepresentation {
|
impl EarlyLintPass for DecimalLiteralRepresentation {
|
||||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||||
if in_external_macro(cx.sess(), expr.span) {
|
if in_external_macro(cx.sess, expr.span) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
|
use clippy_utils::source::snippet_opt;
|
||||||
use clippy_utils::ty::implements_trait;
|
use clippy_utils::ty::implements_trait;
|
||||||
use clippy_utils::{is_expr_path_def_path, paths, sugg};
|
use clippy_utils::{is_expr_path_def_path, paths, sugg};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_lint::{LateContext, LintContext};
|
use rustc_lint::LateContext;
|
||||||
use rustc_middle::ty::Ty;
|
use rustc_middle::ty::Ty;
|
||||||
use rustc_span::sym;
|
use rustc_span::sym;
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ fn extract_turbofish(cx: &LateContext<'_>, expr: &hir::Expr<'_>, ty: Ty<'tcx>) -
|
|||||||
|
|
||||||
let call_site = expr.span.source_callsite();
|
let call_site = expr.span.source_callsite();
|
||||||
if_chain! {
|
if_chain! {
|
||||||
if let Ok(snippet) = cx.sess().source_map().span_to_snippet(call_site);
|
if let Some(snippet) = snippet_opt(cx, call_site);
|
||||||
let snippet_split = snippet.split("::").collect::<Vec<_>>();
|
let snippet_split = snippet.split("::").collect::<Vec<_>>();
|
||||||
if let Some((_, elements)) = snippet_split.split_last();
|
if let Some((_, elements)) = snippet_split.split_last();
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ use clippy_utils::source::snippet_opt;
|
|||||||
use rustc_ast::ast::{Expr, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind};
|
use rustc_ast::ast::{Expr, Generics, Lit, LitFloatType, LitIntType, LitKind, NodeId, Pat, PatKind};
|
||||||
use rustc_ast::visit::FnKind;
|
use rustc_ast::visit::FnKind;
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::source_map::Span;
|
use rustc_span::source_map::Span;
|
||||||
@ -307,7 +307,7 @@ impl EarlyLintPass for MiscEarlyLints {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
|
||||||
if in_external_macro(cx.sess(), expr.span) {
|
if in_external_macro(cx.sess, expr.span) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
double_neg::check(cx, expr);
|
double_neg::check(cx, expr);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
|
||||||
|
use clippy_utils::source::snippet_opt;
|
||||||
use rustc_ast::ast::{Pat, PatKind};
|
use rustc_ast::ast::{Pat, PatKind};
|
||||||
use rustc_lint::{EarlyContext, LintContext};
|
use rustc_lint::EarlyContext;
|
||||||
|
|
||||||
use super::UNNEEDED_FIELD_PATTERN;
|
use super::UNNEEDED_FIELD_PATTERN;
|
||||||
|
|
||||||
@ -48,7 +49,7 @@ pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
|
|||||||
match field.pat.kind {
|
match field.pat.kind {
|
||||||
PatKind::Wild => {},
|
PatKind::Wild => {},
|
||||||
_ => {
|
_ => {
|
||||||
if let Ok(n) = cx.sess().source_map().span_to_snippet(field.span) {
|
if let Some(n) = snippet_opt(cx, field.span) {
|
||||||
normal.push(n);
|
normal.push(n);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -8,7 +8,7 @@ use rustc_errors::Applicability;
|
|||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::intravisit as hir_visit;
|
use rustc_hir::intravisit as hir_visit;
|
||||||
use rustc_hir::intravisit::Visitor as HirVisitor;
|
use rustc_hir::intravisit::Visitor as HirVisitor;
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
|
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
|
||||||
use rustc_middle::hir::map::Map;
|
use rustc_middle::hir::map::Map;
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
@ -63,7 +63,7 @@ impl<'ast> ast_visit::Visitor<'ast> for ReturnVisitor {
|
|||||||
|
|
||||||
impl EarlyLintPass for RedundantClosureCall {
|
impl EarlyLintPass for RedundantClosureCall {
|
||||||
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
|
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
|
||||||
if in_external_macro(cx.sess(), expr.span) {
|
if in_external_macro(cx.sess, expr.span) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if_chain! {
|
if_chain! {
|
||||||
|
@ -54,7 +54,8 @@ impl EarlyLintPass for DerefAddrOf {
|
|||||||
then {
|
then {
|
||||||
let mut applicability = Applicability::MachineApplicable;
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
let sugg = if e.span.from_expansion() {
|
let sugg = if e.span.from_expansion() {
|
||||||
if let Ok(macro_source) = cx.sess.source_map().span_to_snippet(e.span) {
|
#[allow(clippy::option_if_let_else)]
|
||||||
|
if let Some(macro_source) = snippet_opt(cx, e.span) {
|
||||||
// Remove leading whitespace from the given span
|
// Remove leading whitespace from the given span
|
||||||
// e.g: ` $visitor` turns into `$visitor`
|
// e.g: ` $visitor` turns into `$visitor`
|
||||||
let trim_leading_whitespaces = |span| {
|
let trim_leading_whitespaces = |span| {
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::source::position_before_rarrow;
|
use clippy_utils::source::{position_before_rarrow, snippet_opt};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_ast::ast;
|
use rustc_ast::ast;
|
||||||
use rustc_ast::visit::FnKind;
|
use rustc_ast::visit::FnKind;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::source_map::Span;
|
use rustc_span::source_map::Span;
|
||||||
use rustc_span::BytePos;
|
use rustc_span::BytePos;
|
||||||
@ -125,17 +125,16 @@ fn is_unit_expr(expr: &ast::Expr) -> bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn lint_unneeded_unit_return(cx: &EarlyContext<'_>, ty: &ast::Ty, span: Span) {
|
fn lint_unneeded_unit_return(cx: &EarlyContext<'_>, ty: &ast::Ty, span: Span) {
|
||||||
let (ret_span, appl) = if let Ok(fn_source) = cx.sess().source_map().span_to_snippet(span.with_hi(ty.span.hi())) {
|
let (ret_span, appl) =
|
||||||
position_before_rarrow(&fn_source).map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
|
snippet_opt(cx, span.with_hi(ty.span.hi())).map_or((ty.span, Applicability::MaybeIncorrect), |fn_source| {
|
||||||
(
|
position_before_rarrow(&fn_source).map_or((ty.span, Applicability::MaybeIncorrect), |rpos| {
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
(
|
||||||
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
Applicability::MachineApplicable,
|
ty.span.with_lo(BytePos(span.lo().0 + rpos as u32)),
|
||||||
)
|
Applicability::MachineApplicable,
|
||||||
})
|
)
|
||||||
} else {
|
})
|
||||||
(ty.span, Applicability::MaybeIncorrect)
|
});
|
||||||
};
|
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
UNUSED_UNIT,
|
UNUSED_UNIT,
|
||||||
|
Loading…
Reference in New Issue
Block a user