diff --git a/clippy_lints/src/derive.rs b/clippy_lints/src/derive.rs index f130a69778f6..eaff716e1450 100644 --- a/clippy_lints/src/derive.rs +++ b/clippy_lints/src/derive.rs @@ -94,7 +94,8 @@ fn check_hash_peq<'a, 'tcx>( if_chain! { if match_path(&trait_ref.path, &paths::HASH); if let Some(peq_trait_def_id) = cx.tcx.lang_items().eq_trait(); - if !&trait_ref.trait_def_id().is_local(); + if let Some(def_id) = &trait_ref.trait_def_id(); + if !def_id.is_local(); then { // Look for the PartialEq implementations for `ty` cx.tcx.for_each_relevant_impl(peq_trait_def_id, ty, |impl_id| { diff --git a/clippy_lints/src/if_let_some_result.rs b/clippy_lints/src/if_let_some_result.rs index 33e2471d5b8f..ebc8aa419378 100644 --- a/clippy_lints/src/if_let_some_result.rs +++ b/clippy_lints/src/if_let_some_result.rs @@ -1,7 +1,8 @@ use crate::utils::{match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_sugg}; use if_chain::if_chain; use rustc_errors::Applicability; -use rustc_hir::{print, Expr, ExprKind, MatchSource, PatKind, QPath}; +use rustc_hir::{Expr, ExprKind, MatchSource, PatKind, QPath}; +use rustc_hir_pretty; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -46,7 +47,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OkIfLet { if let PatKind::TupleStruct(QPath::Resolved(_, ref x), ref y, _) = body[0].pat.kind; //get operation if method_chain_args(op, &["ok"]).is_some(); //test to see if using ok() methoduse std::marker::Sized; let is_result_type = match_type(cx, cx.tables.expr_ty(&result_types[0]), &paths::RESULT); - if print::to_string(print::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type; + if rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(x, false)) == "Some" && is_result_type; then { let mut applicability = Applicability::MachineApplicable; diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index b701f4fee315..108e28235c42 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -34,6 +34,8 @@ extern crate rustc_errors; #[allow(unused_extern_crates)] extern crate rustc_hir; #[allow(unused_extern_crates)] +extern crate rustc_hir_pretty; +#[allow(unused_extern_crates)] extern crate rustc_index; #[allow(unused_extern_crates)] extern crate rustc_infer; diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 40880f78270c..4892feba3ba7 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -15,9 +15,10 @@ use rustc_ast::ast::LitKind; use rustc_errors::Applicability; use rustc_hir::def::CtorKind; use rustc_hir::{ - print, Arm, BindingAnnotation, Block, BorrowKind, Expr, ExprKind, Local, MatchSource, Mutability, Node, Pat, - PatKind, QPath, RangeEnd, + Arm, BindingAnnotation, Block, BorrowKind, Expr, ExprKind, Local, MatchSource, Mutability, Node, Pat, PatKind, + QPath, RangeEnd, }; +use rustc_hir_pretty; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_span::source_map::Span; @@ -536,10 +537,12 @@ fn check_single_match_opt_like( if !inner.iter().all(is_wild) { return; } - print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)) + rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)) }, PatKind::Binding(BindingAnnotation::Unannotated, .., ident, None) => ident.to_string(), - PatKind::Path(ref path) => print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)), + PatKind::Path(ref path) => { + rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)) + }, _ => return, }; @@ -638,7 +641,7 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>]) if match_type(cx, ex_ty, &paths::RESULT) { for arm in arms { if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pat.kind { - let path_str = print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)); + let path_str = rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)); if path_str == "Err" { let mut matching_wild = inner.iter().any(is_wild); let mut ident_bind_name = String::from("_"); diff --git a/clippy_lints/src/mut_reference.rs b/clippy_lints/src/mut_reference.rs index aeffcd34110e..316df21e7805 100644 --- a/clippy_lints/src/mut_reference.rs +++ b/clippy_lints/src/mut_reference.rs @@ -1,7 +1,8 @@ use crate::utils::span_lint; use rustc::ty::subst::Subst; use rustc::ty::{self, Ty}; -use rustc_hir::{print, BorrowKind, Expr, ExprKind, Mutability}; +use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability}; +use rustc_hir_pretty; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -34,7 +35,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnecessaryMutPassed { cx, arguments, cx.tables.expr_ty(fn_expr), - &print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)), + &rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)), ); } }, diff --git a/clippy_lints/src/trait_bounds.rs b/clippy_lints/src/trait_bounds.rs index d343ec4a8483..075df19a71e2 100644 --- a/clippy_lints/src/trait_bounds.rs +++ b/clippy_lints/src/trait_bounds.rs @@ -1,5 +1,6 @@ -use crate::utils::{in_macro, snippet, span_lint_and_help, SpanlessHash}; +use crate::utils::{in_macro, snippet, snippet_with_applicability, span_lint_and_help, SpanlessHash}; use rustc_data_structures::fx::FxHashMap; +use rustc_errors::Applicability; use rustc_hir::{GenericBound, Generics, WherePredicate}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_tool_lint, impl_lint_pass}; @@ -41,6 +42,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TraitBounds { hasher.finish() }; let mut map = FxHashMap::default(); + let mut applicability = Applicability::MaybeIncorrect; for bound in gen.where_clause.predicates { if let WherePredicate::BoundPredicate(ref p) = bound { let h = hash(&p.bounded_ty); @@ -52,13 +54,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TraitBounds { for b in v.iter() { if let GenericBound::Trait(ref poly_trait_ref, _) = b { let path = &poly_trait_ref.trait_ref.path; - hint_string.push_str(&format!(" {} +", path)); + hint_string.push_str(&format!( + " {} +", + snippet_with_applicability(cx, path.span, "..", &mut applicability) + )); } } for b in p.bounds.iter() { if let GenericBound::Trait(ref poly_trait_ref, _) = b { let path = &poly_trait_ref.trait_ref.path; - hint_string.push_str(&format!(" {} +", path)); + hint_string.push_str(&format!( + " {} +", + snippet_with_applicability(cx, path.span, "..", &mut applicability) + )); } } hint_string.truncate(hint_string.len() - 2); diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index c6a47f05e248..085184015e3e 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -3,7 +3,7 @@ use crate::utils::get_attr; use rustc_ast::ast::Attribute; use rustc_hir as hir; -use rustc_hir::print; +use rustc_hir_pretty; use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_session::Session; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -50,7 +50,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DeepCodeInspector { hir::VisibilityKind::Crate(_) => println!("visible crate wide"), hir::VisibilityKind::Restricted { ref path, .. } => println!( "visible in module `{}`", - print::to_string(print::NO_ANN, |s| s.print_path(path, false)) + rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(path, false)) ), hir::VisibilityKind::Inherited => println!("visibility inherited from outer item"), } @@ -333,7 +333,7 @@ fn print_item(cx: &LateContext<'_, '_>, item: &hir::Item<'_>) { hir::VisibilityKind::Crate(_) => println!("visible crate wide"), hir::VisibilityKind::Restricted { ref path, .. } => println!( "visible in module `{}`", - print::to_string(print::NO_ANN, |s| s.print_path(path, false)) + rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_path(path, false)) ), hir::VisibilityKind::Inherited => println!("visibility inherited from outer item"), } @@ -427,7 +427,7 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat<'_>, indent: usize) { println!( "{}name: {}", ind, - print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)) + rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)) ); println!("{}ignore leftover fields: {}", ind, ignore); println!("{}fields:", ind); @@ -444,7 +444,7 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat<'_>, indent: usize) { println!( "{}path: {}", ind, - print::to_string(print::NO_ANN, |s| s.print_qpath(path, false)) + rustc_hir_pretty::to_string(rustc_hir_pretty::NO_ANN, |s| s.print_qpath(path, false)) ); if let Some(dot_position) = opt_dots_position { println!("{}dot position: {}", ind, dot_position);