Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaskrgr committed Mar 27, 2020
1 parent 2ff568d commit 8177e49
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 18 deletions.
3 changes: 2 additions & 1 deletion clippy_lints/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/if_let_some_result.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 8 additions & 5 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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("_");
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/mut_reference.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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)),
);
}
},
Expand Down
14 changes: 11 additions & 3 deletions clippy_lints/src/trait_bounds.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/utils/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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"),
}
Expand Down Expand Up @@ -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"),
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 8177e49

Please sign in to comment.