This commit is contained in:
Matthias Krüger 2020-03-27 15:34:29 +01:00
parent 2ff568d746
commit 8177e49e10
7 changed files with 34 additions and 18 deletions

View File

@ -94,7 +94,8 @@ fn check_hash_peq<'a, 'tcx>(
if_chain! { if_chain! {
if match_path(&trait_ref.path, &paths::HASH); if match_path(&trait_ref.path, &paths::HASH);
if let Some(peq_trait_def_id) = cx.tcx.lang_items().eq_trait(); 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 { then {
// Look for the PartialEq implementations for `ty` // Look for the PartialEq implementations for `ty`
cx.tcx.for_each_relevant_impl(peq_trait_def_id, ty, |impl_id| { cx.tcx.for_each_relevant_impl(peq_trait_def_id, ty, |impl_id| {

View File

@ -1,7 +1,8 @@
use crate::utils::{match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_sugg}; use crate::utils::{match_type, method_chain_args, paths, snippet_with_applicability, span_lint_and_sugg};
use if_chain::if_chain; use if_chain::if_chain;
use rustc_errors::Applicability; 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_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; 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 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; 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); 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 { then {
let mut applicability = Applicability::MachineApplicable; let mut applicability = Applicability::MachineApplicable;

View File

@ -34,6 +34,8 @@ extern crate rustc_errors;
#[allow(unused_extern_crates)] #[allow(unused_extern_crates)]
extern crate rustc_hir; extern crate rustc_hir;
#[allow(unused_extern_crates)] #[allow(unused_extern_crates)]
extern crate rustc_hir_pretty;
#[allow(unused_extern_crates)]
extern crate rustc_index; extern crate rustc_index;
#[allow(unused_extern_crates)] #[allow(unused_extern_crates)]
extern crate rustc_infer; extern crate rustc_infer;

View File

@ -15,9 +15,10 @@ use rustc_ast::ast::LitKind;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::CtorKind; use rustc_hir::def::CtorKind;
use rustc_hir::{ use rustc_hir::{
print, Arm, BindingAnnotation, Block, BorrowKind, Expr, ExprKind, Local, MatchSource, Mutability, Node, Pat, Arm, BindingAnnotation, Block, BorrowKind, Expr, ExprKind, Local, MatchSource, Mutability, Node, Pat, PatKind,
PatKind, QPath, RangeEnd, QPath, RangeEnd,
}; };
use rustc_hir_pretty;
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
@ -536,10 +537,12 @@ fn check_single_match_opt_like(
if !inner.iter().all(is_wild) { if !inner.iter().all(is_wild) {
return; 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::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, _ => return,
}; };
@ -638,7 +641,7 @@ fn check_wild_err_arm(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>])
if match_type(cx, ex_ty, &paths::RESULT) { if match_type(cx, ex_ty, &paths::RESULT) {
for arm in arms { for arm in arms {
if let PatKind::TupleStruct(ref path, ref inner, _) = arm.pat.kind { 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" { if path_str == "Err" {
let mut matching_wild = inner.iter().any(is_wild); let mut matching_wild = inner.iter().any(is_wild);
let mut ident_bind_name = String::from("_"); let mut ident_bind_name = String::from("_");

View File

@ -1,7 +1,8 @@
use crate::utils::span_lint; use crate::utils::span_lint;
use rustc::ty::subst::Subst; use rustc::ty::subst::Subst;
use rustc::ty::{self, Ty}; 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_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -34,7 +35,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnnecessaryMutPassed {
cx, cx,
arguments, arguments,
cx.tables.expr_ty(fn_expr), 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)),
); );
} }
}, },

View File

@ -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_data_structures::fx::FxHashMap;
use rustc_errors::Applicability;
use rustc_hir::{GenericBound, Generics, WherePredicate}; use rustc_hir::{GenericBound, Generics, WherePredicate};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, impl_lint_pass}; use rustc_session::{declare_tool_lint, impl_lint_pass};
@ -41,6 +42,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TraitBounds {
hasher.finish() hasher.finish()
}; };
let mut map = FxHashMap::default(); let mut map = FxHashMap::default();
let mut applicability = Applicability::MaybeIncorrect;
for bound in gen.where_clause.predicates { for bound in gen.where_clause.predicates {
if let WherePredicate::BoundPredicate(ref p) = bound { if let WherePredicate::BoundPredicate(ref p) = bound {
let h = hash(&p.bounded_ty); let h = hash(&p.bounded_ty);
@ -52,13 +54,19 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TraitBounds {
for b in v.iter() { for b in v.iter() {
if let GenericBound::Trait(ref poly_trait_ref, _) = b { if let GenericBound::Trait(ref poly_trait_ref, _) = b {
let path = &poly_trait_ref.trait_ref.path; 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() { for b in p.bounds.iter() {
if let GenericBound::Trait(ref poly_trait_ref, _) = b { if let GenericBound::Trait(ref poly_trait_ref, _) = b {
let path = &poly_trait_ref.trait_ref.path; 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); hint_string.truncate(hint_string.len() - 2);

View File

@ -3,7 +3,7 @@
use crate::utils::get_attr; use crate::utils::get_attr;
use rustc_ast::ast::Attribute; use rustc_ast::ast::Attribute;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::print; use rustc_hir_pretty;
use rustc_lint::{LateContext, LateLintPass, LintContext}; use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_session::Session; use rustc_session::Session;
use rustc_session::{declare_lint_pass, declare_tool_lint}; 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::Crate(_) => println!("visible crate wide"),
hir::VisibilityKind::Restricted { ref path, .. } => println!( hir::VisibilityKind::Restricted { ref path, .. } => println!(
"visible in module `{}`", "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"), 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::Crate(_) => println!("visible crate wide"),
hir::VisibilityKind::Restricted { ref path, .. } => println!( hir::VisibilityKind::Restricted { ref path, .. } => println!(
"visible in module `{}`", "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"), hir::VisibilityKind::Inherited => println!("visibility inherited from outer item"),
} }
@ -427,7 +427,7 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat<'_>, indent: usize) {
println!( println!(
"{}name: {}", "{}name: {}",
ind, 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!("{}ignore leftover fields: {}", ind, ignore);
println!("{}fields:", ind); println!("{}fields:", ind);
@ -444,7 +444,7 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat<'_>, indent: usize) {
println!( println!(
"{}path: {}", "{}path: {}",
ind, 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 { if let Some(dot_position) = opt_dots_position {
println!("{}dot position: {}", ind, dot_position); println!("{}dot position: {}", ind, dot_position);