Skip to content

Commit

Permalink
Work around qpath_res issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Wright committed Sep 18, 2019
1 parent c3cfb77 commit 16ce071
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 63 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/drop_forget_ref.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{is_copy, match_def_path, paths, span_note_and_lint};
use crate::utils::{is_copy, match_def_path, paths, qpath_res, span_note_and_lint};
use if_chain::if_chain;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
Expand Down Expand Up @@ -114,7 +114,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DropForgetRef {
if let ExprKind::Call(ref path, ref args) = expr.node;
if let ExprKind::Path(ref qpath) = path.node;
if args.len() == 1;
if let Some(def_id) = cx.tables.qpath_res(qpath, path.hir_id).opt_def_id();
if let Some(def_id) = qpath_res(cx, qpath, path.hir_id).opt_def_id();
then {
let lint;
let msg;
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::convert::TryFrom;

use crate::utils::{iter_input_pats, snippet, snippet_opt, span_lint, type_is_unsafe_function};
use crate::utils::{iter_input_pats, qpath_res, snippet, snippet_opt, span_lint, type_is_unsafe_function};
use matches::matches;
use rustc::hir;
use rustc::hir::def::Res;
Expand Down Expand Up @@ -318,7 +318,7 @@ impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for DerefVisitor<'a, 'tcx> {
impl<'a, 'tcx> DerefVisitor<'a, 'tcx> {
fn check_arg(&self, ptr: &hir::Expr) {
if let hir::ExprKind::Path(ref qpath) = ptr.node {
if let Res::Local(id) = self.cx.tables.qpath_res(qpath, ptr.hir_id) {
if let Res::Local(id) = qpath_res(self.cx, qpath, ptr.hir_id) {
if self.ptrs.contains(&id) {
span_lint(
self.cx,
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/let_if_seq.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{higher, snippet, span_lint_and_then};
use crate::utils::{higher, qpath_res, snippet, span_lint_and_then};
use if_chain::if_chain;
use rustc::hir;
use rustc::hir::def::Res;
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<'a, 'tcx> hir::intravisit::Visitor<'tcx> for UsedVisitor<'a, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
if_chain! {
if let hir::ExprKind::Path(ref qpath) = expr.node;
if let Res::Local(local_id) = self.cx.tables.qpath_res(qpath, expr.hir_id);
if let Res::Local(local_id) = qpath_res(self.cx, qpath, expr.hir_id);
if self.id == local_id;
then {
self.used = true;
Expand All @@ -170,7 +170,7 @@ fn check_assign<'a, 'tcx>(
if let hir::StmtKind::Semi(ref expr) = expr.node;
if let hir::ExprKind::Assign(ref var, ref value) = expr.node;
if let hir::ExprKind::Path(ref qpath) = var.node;
if let Res::Local(local_id) = cx.tables.qpath_res(qpath, var.hir_id);
if let Res::Local(local_id) = qpath_res(cx, qpath, var.hir_id);
if decl == local_id;
then {
let mut v = UsedVisitor {
Expand Down
14 changes: 7 additions & 7 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc::{declare_lint_pass, declare_tool_lint};
// use rustc::middle::region::CodeExtent;
use crate::consts::{constant, Constant};
use crate::utils::usage::mutated_variables;
use crate::utils::{is_type_diagnostic_item, sext, sugg};
use crate::utils::{is_type_diagnostic_item, qpath_res, sext, sugg};
use rustc::middle::expr_use_visitor::*;
use rustc::middle::mem_categorization::cmt_;
use rustc::middle::mem_categorization::Categorization;
Expand Down Expand Up @@ -754,7 +754,7 @@ fn same_var<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &Expr, var: HirId) -> bo
if let ExprKind::Path(ref qpath) = expr.node;
if let QPath::Resolved(None, ref path) = *qpath;
if path.segments.len() == 1;
if let Res::Local(local_id) = cx.tables.qpath_res(qpath, expr.hir_id);
if let Res::Local(local_id) = qpath_res(cx, qpath, expr.hir_id);
// our variable!
if local_id == var;
then {
Expand Down Expand Up @@ -1618,7 +1618,7 @@ fn check_for_mutability(cx: &LateContext<'_, '_>, bound: &Expr) -> Option<HirId>
if let ExprKind::Path(ref qpath) = bound.node;
if let QPath::Resolved(None, _) = *qpath;
then {
let res = cx.tables.qpath_res(qpath, bound.hir_id);
let res = qpath_res(cx, qpath, bound.hir_id);
if let Res::Local(node_id) = res {
let node_str = cx.tcx.hir().get(node_id);
if_chain! {
Expand Down Expand Up @@ -1762,7 +1762,7 @@ impl<'a, 'tcx> VarVisitor<'a, 'tcx> {
if self.prefer_mutable {
self.indexed_mut.insert(seqvar.segments[0].ident.name);
}
let res = self.cx.tables.qpath_res(seqpath, seqexpr.hir_id);
let res = qpath_res(self.cx, seqpath, seqexpr.hir_id);
match res {
Res::Local(hir_id) => {
let parent_id = self.cx.tcx.hir().get_parent_item(expr.hir_id);
Expand Down Expand Up @@ -1824,7 +1824,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
if let QPath::Resolved(None, ref path) = *qpath;
if path.segments.len() == 1;
then {
if let Res::Local(local_id) = self.cx.tables.qpath_res(qpath, expr.hir_id) {
if let Res::Local(local_id) = qpath_res(self.cx, qpath, expr.hir_id) {
if local_id == self.var {
self.nonindex = true;
} else {
Expand Down Expand Up @@ -2163,7 +2163,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {

fn var_def_id(cx: &LateContext<'_, '_>, expr: &Expr) -> Option<HirId> {
if let ExprKind::Path(ref qpath) = expr.node {
let path_res = cx.tables.qpath_res(qpath, expr.hir_id);
let path_res = qpath_res(cx, qpath, expr.hir_id);
if let Res::Local(node_id) = path_res {
return Some(node_id);
}
Expand Down Expand Up @@ -2355,7 +2355,7 @@ impl<'a, 'tcx> VarCollectorVisitor<'a, 'tcx> {
if_chain! {
if let ExprKind::Path(ref qpath) = ex.node;
if let QPath::Resolved(None, _) = *qpath;
let res = self.cx.tables.qpath_res(qpath, ex.hir_id);
let res = qpath_res(self.cx, qpath, ex.hir_id);
then {
match res {
Res::Local(node_id) => {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/mem_forget.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{match_def_path, paths, span_lint};
use crate::utils::{match_def_path, paths, qpath_res, span_lint};
use rustc::hir::{Expr, ExprKind};
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -29,7 +29,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemForget {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
if let ExprKind::Call(ref path_expr, ref args) = e.node {
if let ExprKind::Path(ref qpath) = path_expr.node {
if let Some(def_id) = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
if let Some(def_id) = qpath_res(cx, qpath, path_expr.hir_id).opt_def_id() {
if match_def_path(cx, def_id, &paths::MEM_FORGET) {
let forgot_ty = cx.tables.expr_ty(&args[0]);

Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/no_effect.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{has_drop, snippet_opt, span_lint, span_lint_and_sugg};
use crate::utils::{has_drop, qpath_res, snippet_opt, span_lint, span_lint_and_sugg};
use rustc::hir::def::{DefKind, Res};
use rustc::hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource};
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
Expand Down Expand Up @@ -67,7 +67,7 @@ fn has_no_effect(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
},
ExprKind::Call(ref callee, ref args) => {
if let ExprKind::Path(ref qpath) = callee.node {
let res = cx.tables.qpath_res(qpath, callee.hir_id);
let res = qpath_res(cx, qpath, callee.hir_id);
match res {
Res::Def(DefKind::Struct, ..) | Res::Def(DefKind::Variant, ..) | Res::Def(DefKind::Ctor(..), _) => {
!has_drop(cx, cx.tables.expr_ty(expr)) && args.iter().all(|arg| has_no_effect(cx, arg))
Expand Down Expand Up @@ -145,7 +145,7 @@ fn reduce_expression<'a>(cx: &LateContext<'_, '_>, expr: &'a Expr) -> Option<Vec
},
ExprKind::Call(ref callee, ref args) => {
if let ExprKind::Path(ref qpath) = callee.node {
let res = cx.tables.qpath_res(qpath, callee.hir_id);
let res = qpath_res(cx, qpath, callee.hir_id);
match res {
Res::Def(DefKind::Struct, ..) | Res::Def(DefKind::Variant, ..) | Res::Def(DefKind::Ctor(..), _)
if !has_drop(cx, cx.tables.expr_ty(expr)) =>
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/non_copy_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use rustc_errors::Applicability;
use rustc_typeck::hir_ty_to_ty;
use syntax_pos::{InnerSpan, Span, DUMMY_SP};

use crate::utils::{in_constant, is_copy, span_lint_and_then};
use crate::utils::{in_constant, is_copy, qpath_res, span_lint_and_then};

declare_clippy_lint! {
/// **What it does:** Checks for declaration of `const` items which is interior
Expand Down Expand Up @@ -195,7 +195,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonCopyConst {
}

// Make sure it is a const item.
match cx.tables.qpath_res(qpath, expr.hir_id) {
match qpath_res(cx, qpath, expr.hir_id) {
Res::Def(DefKind::Const, _) | Res::Def(DefKind::AssocConst, _) => {},
_ => return,
};
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::consts::{constant, Constant};
use crate::utils::paths;
use crate::utils::{
clip, comparisons, differing_macro_contexts, higher, in_constant, int_bits, last_path_segment, match_def_path,
match_path, multispan_sugg, same_tys, sext, snippet, snippet_opt, snippet_with_applicability,
match_path, multispan_sugg, qpath_res, same_tys, sext, snippet, snippet_opt, snippet_with_applicability,
snippet_with_macro_callsite, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext,
};

Expand Down Expand Up @@ -218,7 +218,7 @@ fn match_type_parameter(cx: &LateContext<'_, '_>, qpath: &QPath, path: &[&str])
_ => None,
});
if let TyKind::Path(ref qpath) = ty.node;
if let Some(did) = cx.tables.qpath_res(qpath, ty.hir_id).opt_def_id();
if let Some(did) = qpath_res(cx, qpath, ty.hir_id).opt_def_id();
if match_def_path(cx, did, path);
then {
return true;
Expand All @@ -240,7 +240,7 @@ fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) {
match hir_ty.node {
TyKind::Path(ref qpath) if !is_local => {
let hir_id = hir_ty.hir_id;
let res = cx.tables.qpath_res(qpath, hir_id);
let res = qpath_res(cx, qpath, hir_id);
if let Some(def_id) = res.opt_def_id() {
if Some(def_id) == cx.tcx.lang_items().owned_box() {
if match_type_parameter(cx, qpath, &paths::VEC) {
Expand All @@ -263,7 +263,7 @@ fn check_ty(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool) {
});
// ty is now _ at this point
if let TyKind::Path(ref ty_qpath) = ty.node;
let res = cx.tables.qpath_res(ty_qpath, ty.hir_id);
let res = qpath_res(cx, ty_qpath, ty.hir_id);
if let Some(def_id) = res.opt_def_id();
if Some(def_id) == cx.tcx.lang_items().owned_box();
// At this point, we know ty is Box<T>, now get T
Expand Down Expand Up @@ -369,7 +369,7 @@ fn check_ty_rptr(cx: &LateContext<'_, '_>, hir_ty: &hir::Ty, is_local: bool, lt:
match mut_ty.ty.node {
TyKind::Path(ref qpath) => {
let hir_id = mut_ty.ty.hir_id;
let def = cx.tables.qpath_res(qpath, hir_id);
let def = qpath_res(cx, qpath, hir_id);
if_chain! {
if let Some(def_id) = def.opt_def_id();
if Some(def_id) == cx.tcx.lang_items().owned_box();
Expand Down
13 changes: 13 additions & 0 deletions clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,19 @@ pub fn path_to_res(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<(def::Res)
}
}

pub fn qpath_res(cx: &LateContext<'_, '_>, qpath: &hir::QPath, id: hir::HirId) -> Res {
match qpath {
hir::QPath::Resolved(_, path) => path.res,
hir::QPath::TypeRelative(..) => {
if cx.tcx.has_typeck_tables(id.owner_def_id()) {
cx.tcx.typeck_tables_of(id.owner_def_id()).qpath_res(qpath, id)
} else {
Res::Err
}
},
}
}

/// Convenience function to get the `DefId` of a trait by path.
/// It could be a trait or trait alias.
pub fn get_trait_def_id(cx: &LateContext<'_, '_>, path: &[&str]) -> Option<DefId> {
Expand Down
1 change: 0 additions & 1 deletion tests/ui/ice-4545.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ fn repro() {
fn main() {
repro();
}

1 change: 0 additions & 1 deletion tests/ui/non_copy_const.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(const_string_new, const_vec_new)]
#![allow(clippy::ref_in_deref, dead_code)]

use std::borrow::Cow;
Expand Down
Loading

0 comments on commit 16ce071

Please sign in to comment.