Remove qpath_res util function

This commit is contained in:
Cameron Steffen 2021-01-18 13:36:32 -06:00
parent 9009f8f031
commit 2454408318
14 changed files with 45 additions and 58 deletions

View File

@ -1,5 +1,5 @@
use crate::utils::{ use crate::utils::{
any_parent_is_automatically_derived, contains_name, match_def_path, paths, qpath_res, snippet_with_macro_callsite, any_parent_is_automatically_derived, contains_name, match_def_path, paths, snippet_with_macro_callsite,
}; };
use crate::utils::{span_lint_and_note, span_lint_and_sugg}; use crate::utils::{span_lint_and_note, span_lint_and_sugg};
use if_chain::if_chain; use if_chain::if_chain;
@ -231,7 +231,7 @@ fn is_expr_default<'tcx>(expr: &'tcx Expr<'tcx>, cx: &LateContext<'tcx>) -> bool
if_chain! { if_chain! {
if let ExprKind::Call(ref fn_expr, _) = &expr.kind; if let ExprKind::Call(ref fn_expr, _) = &expr.kind;
if let ExprKind::Path(qpath) = &fn_expr.kind; if let ExprKind::Path(qpath) = &fn_expr.kind;
if let Res::Def(_, def_id) = qpath_res(cx, qpath, fn_expr.hir_id); if let Res::Def(_, def_id) = cx.qpath_res(qpath, fn_expr.hir_id);
then { then {
// right hand side of assignment is `Default::default` // right hand side of assignment is `Default::default`
match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD) match_def_path(cx, def_id, &paths::DEFAULT_TRAIT_METHOD)

View File

@ -1,4 +1,4 @@
use crate::utils::{is_copy, match_def_path, paths, qpath_res, span_lint_and_note}; use crate::utils::{is_copy, match_def_path, paths, span_lint_and_note};
use if_chain::if_chain; use if_chain::if_chain;
use rustc_hir::{Expr, ExprKind}; use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
@ -114,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
if let ExprKind::Call(ref path, ref args) = expr.kind; if let ExprKind::Call(ref path, ref args) = expr.kind;
if let ExprKind::Path(ref qpath) = path.kind; if let ExprKind::Path(ref qpath) = path.kind;
if args.len() == 1; if args.len() == 1;
if let Some(def_id) = qpath_res(cx, qpath, path.hir_id).opt_def_id(); if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
then { then {
let lint; let lint;
let msg; let msg;

View File

@ -1,4 +1,4 @@
use crate::utils::{is_entrypoint_fn, match_def_path, paths, qpath_res, span_lint}; use crate::utils::{is_entrypoint_fn, match_def_path, paths, span_lint};
use if_chain::if_chain; use if_chain::if_chain;
use rustc_hir::{Expr, ExprKind, Item, ItemKind, Node}; use rustc_hir::{Expr, ExprKind, Item, ItemKind, Node};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
@ -29,7 +29,7 @@ impl<'tcx> LateLintPass<'tcx> for Exit {
if_chain! { if_chain! {
if let ExprKind::Call(ref path_expr, ref _args) = e.kind; if let ExprKind::Call(ref path_expr, ref _args) = e.kind;
if let ExprKind::Path(ref path) = path_expr.kind; if let ExprKind::Path(ref path) = path_expr.kind;
if let Some(def_id) = qpath_res(cx, path, path_expr.hir_id).opt_def_id(); if let Some(def_id) = cx.qpath_res(path, path_expr.hir_id).opt_def_id();
if match_def_path(cx, def_id, &paths::EXIT); if match_def_path(cx, def_id, &paths::EXIT);
then { then {
let parent = cx.tcx.hir().get_parent_item(e.hir_id); let parent = cx.tcx.hir().get_parent_item(e.hir_id);

View File

@ -1,7 +1,7 @@
use crate::utils::{ use crate::utils::{
attr_by_name, attrs::is_proc_macro, is_must_use_ty, is_trait_impl_item, is_type_diagnostic_item, iter_input_pats, attr_by_name, attrs::is_proc_macro, is_must_use_ty, is_trait_impl_item, is_type_diagnostic_item, iter_input_pats,
last_path_segment, match_def_path, must_use_attr, qpath_res, return_ty, snippet, snippet_opt, span_lint, last_path_segment, match_def_path, must_use_attr, return_ty, snippet, snippet_opt, span_lint, span_lint_and_help,
span_lint_and_help, span_lint_and_then, trait_ref_of_method, type_is_unsafe_function, span_lint_and_then, trait_ref_of_method, type_is_unsafe_function,
}; };
use if_chain::if_chain; use if_chain::if_chain;
use rustc_ast::ast::Attribute; use rustc_ast::ast::Attribute;
@ -659,7 +659,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for DerefVisitor<'a, 'tcx> {
impl<'a, 'tcx> DerefVisitor<'a, 'tcx> { impl<'a, 'tcx> DerefVisitor<'a, 'tcx> {
fn check_arg(&self, ptr: &hir::Expr<'_>) { fn check_arg(&self, ptr: &hir::Expr<'_>) {
if let hir::ExprKind::Path(ref qpath) = ptr.kind { if let hir::ExprKind::Path(ref qpath) = ptr.kind {
if let Res::Local(id) = qpath_res(self.cx, qpath, ptr.hir_id) { if let Res::Local(id) = self.cx.qpath_res(qpath, ptr.hir_id) {
if self.ptrs.contains(&id) { if self.ptrs.contains(&id) {
span_lint( span_lint(
self.cx, self.cx,
@ -722,7 +722,7 @@ fn is_mutated_static(cx: &LateContext<'_>, e: &hir::Expr<'_>) -> bool {
use hir::ExprKind::{Field, Index, Path}; use hir::ExprKind::{Field, Index, Path};
match e.kind { match e.kind {
Path(ref qpath) => !matches!(qpath_res(cx, qpath, e.hir_id), Res::Local(_)), Path(ref qpath) => !matches!(cx.qpath_res(qpath, e.hir_id), Res::Local(_)),
Field(ref inner, _) | Index(ref inner, _) => is_mutated_static(cx, inner), Field(ref inner, _) | Index(ref inner, _) => is_mutated_static(cx, inner),
_ => false, _ => false,
} }

View File

@ -1,4 +1,4 @@
use crate::utils::{qpath_res, snippet, span_lint_and_then, visitors::LocalUsedVisitor}; use crate::utils::{snippet, span_lint_and_then, visitors::LocalUsedVisitor};
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;
@ -145,7 +145,7 @@ fn check_assign<'tcx>(
if let hir::StmtKind::Semi(ref expr) = expr.kind; if let hir::StmtKind::Semi(ref expr) = expr.kind;
if let hir::ExprKind::Assign(ref var, ref value, _) = expr.kind; if let hir::ExprKind::Assign(ref var, ref value, _) = expr.kind;
if let hir::ExprKind::Path(ref qpath) = var.kind; if let hir::ExprKind::Path(ref qpath) = var.kind;
if let Res::Local(local_id) = qpath_res(cx, qpath, var.hir_id); if let Res::Local(local_id) = cx.qpath_res(qpath, var.hir_id);
if decl == local_id; if decl == local_id;
then { then {
let mut v = LocalUsedVisitor::new(decl); let mut v = LocalUsedVisitor::new(decl);

View File

@ -6,9 +6,9 @@ use crate::utils::visitors::LocalUsedVisitor;
use crate::utils::{ use crate::utils::{
contains_name, get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait, contains_name, get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
indent_of, is_in_panic_handler, is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item, indent_of, is_in_panic_handler, is_integer_const, is_no_std_crate, is_refutable, is_type_diagnostic_item,
last_path_segment, match_trait_method, match_type, match_var, multispan_sugg, qpath_res, single_segment_path, last_path_segment, match_trait_method, match_type, match_var, multispan_sugg, single_segment_path, snippet,
snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg,
span_lint_and_sugg, span_lint_and_then, sugg, SpanlessEq, span_lint_and_then, sugg, SpanlessEq,
}; };
use if_chain::if_chain; use if_chain::if_chain;
use rustc_ast::ast; use rustc_ast::ast;
@ -848,7 +848,7 @@ fn same_var<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, var: HirId) -> bool {
if let ExprKind::Path(qpath) = &expr.kind; if let ExprKind::Path(qpath) = &expr.kind;
if let QPath::Resolved(None, path) = qpath; if let QPath::Resolved(None, path) = qpath;
if path.segments.len() == 1; if path.segments.len() == 1;
if let Res::Local(local_id) = qpath_res(cx, qpath, expr.hir_id); if let Res::Local(local_id) = cx.qpath_res(qpath, expr.hir_id);
then { then {
// our variable! // our variable!
local_id == var local_id == var
@ -1420,7 +1420,7 @@ fn detect_same_item_push<'tcx>(
// Make sure that the push does not involve possibly mutating values // Make sure that the push does not involve possibly mutating values
match pushed_item.kind { match pushed_item.kind {
ExprKind::Path(ref qpath) => { ExprKind::Path(ref qpath) => {
match qpath_res(cx, qpath, pushed_item.hir_id) { match cx.qpath_res(qpath, pushed_item.hir_id) {
// immutable bindings that are initialized with literal or constant // immutable bindings that are initialized with literal or constant
Res::Local(hir_id) => { Res::Local(hir_id) => {
if_chain! { if_chain! {
@ -1437,7 +1437,7 @@ fn detect_same_item_push<'tcx>(
ExprKind::Lit(..) => emit_lint(cx, vec, pushed_item), ExprKind::Lit(..) => emit_lint(cx, vec, pushed_item),
// immutable bindings that are initialized with constant // immutable bindings that are initialized with constant
ExprKind::Path(ref path) => { ExprKind::Path(ref path) => {
if let Res::Def(DefKind::Const, ..) = qpath_res(cx, path, init.hir_id) { if let Res::Def(DefKind::Const, ..) = cx.qpath_res(path, init.hir_id) {
emit_lint(cx, vec, pushed_item); emit_lint(cx, vec, pushed_item);
} }
} }
@ -2028,7 +2028,7 @@ fn check_for_mutability(cx: &LateContext<'_>, bound: &Expr<'_>) -> Option<HirId>
if let ExprKind::Path(ref qpath) = bound.kind; if let ExprKind::Path(ref qpath) = bound.kind;
if let QPath::Resolved(None, _) = *qpath; if let QPath::Resolved(None, _) = *qpath;
then { then {
let res = qpath_res(cx, qpath, bound.hir_id); let res = cx.qpath_res(qpath, bound.hir_id);
if let Res::Local(hir_id) = res { if let Res::Local(hir_id) = res {
let node_str = cx.tcx.hir().get(hir_id); let node_str = cx.tcx.hir().get(hir_id);
if_chain! { if_chain! {
@ -2120,7 +2120,7 @@ impl<'a, 'tcx> VarVisitor<'a, 'tcx> {
if self.prefer_mutable { if self.prefer_mutable {
self.indexed_mut.insert(seqvar.segments[0].ident.name); self.indexed_mut.insert(seqvar.segments[0].ident.name);
} }
let res = qpath_res(self.cx, seqpath, seqexpr.hir_id); let res = self.cx.qpath_res(seqpath, seqexpr.hir_id);
match res { match res {
Res::Local(hir_id) => { Res::Local(hir_id) => {
let parent_id = self.cx.tcx.hir().get_parent_item(expr.hir_id); let parent_id = self.cx.tcx.hir().get_parent_item(expr.hir_id);
@ -2184,7 +2184,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
if let QPath::Resolved(None, ref path) = *qpath; if let QPath::Resolved(None, ref path) = *qpath;
if path.segments.len() == 1; if path.segments.len() == 1;
then { then {
if let Res::Local(local_id) = qpath_res(self.cx, qpath, expr.hir_id) { if let Res::Local(local_id) = self.cx.qpath_res(qpath, expr.hir_id) {
if local_id == self.var { if local_id == self.var {
self.nonindex = true; self.nonindex = true;
} else { } else {
@ -2589,7 +2589,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
fn var_def_id(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<HirId> { fn var_def_id(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<HirId> {
if let ExprKind::Path(ref qpath) = expr.kind { if let ExprKind::Path(ref qpath) = expr.kind {
let path_res = qpath_res(cx, qpath, expr.hir_id); let path_res = cx.qpath_res(qpath, expr.hir_id);
if let Res::Local(hir_id) = path_res { if let Res::Local(hir_id) = path_res {
return Some(hir_id); return Some(hir_id);
} }
@ -2819,7 +2819,7 @@ impl<'a, 'tcx> VarCollectorVisitor<'a, 'tcx> {
if_chain! { if_chain! {
if let ExprKind::Path(ref qpath) = ex.kind; if let ExprKind::Path(ref qpath) = ex.kind;
if let QPath::Resolved(None, _) = *qpath; if let QPath::Resolved(None, _) = *qpath;
let res = qpath_res(self.cx, qpath, ex.hir_id); let res = self.cx.qpath_res(qpath, ex.hir_id);
then { then {
match res { match res {
Res::Local(hir_id) => { Res::Local(hir_id) => {

View File

@ -1,7 +1,7 @@
use crate::consts::{constant, Constant}; use crate::consts::{constant, Constant};
use crate::utils::usage::mutated_variables; use crate::utils::usage::mutated_variables;
use crate::utils::{ use crate::utils::{
eq_expr_value, higher, match_def_path, meets_msrv, multispan_sugg, paths, qpath_res, snippet, span_lint_and_then, eq_expr_value, higher, match_def_path, meets_msrv, multispan_sugg, paths, snippet, span_lint_and_then,
}; };
use if_chain::if_chain; use if_chain::if_chain;
@ -92,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
} else { } else {
return; return;
}; };
let target_res = qpath_res(cx, &target_path, target_arg.hir_id); let target_res = cx.qpath_res(&target_path, target_arg.hir_id);
if target_res == Res::Err { if target_res == Res::Err {
return; return;
}; };
@ -221,7 +221,7 @@ fn find_stripping<'tcx>(
if let ExprKind::Index(indexed, index) = &unref.kind; if let ExprKind::Index(indexed, index) = &unref.kind;
if let Some(higher::Range { start, end, .. }) = higher::range(index); if let Some(higher::Range { start, end, .. }) = higher::range(index);
if let ExprKind::Path(path) = &indexed.kind; if let ExprKind::Path(path) = &indexed.kind;
if qpath_res(self.cx, path, ex.hir_id) == self.target; if self.cx.qpath_res(path, ex.hir_id) == self.target;
then { then {
match (self.strip_kind, start, end) { match (self.strip_kind, start, end) {
(StripKind::Prefix, Some(start), None) => { (StripKind::Prefix, Some(start), None) => {
@ -235,7 +235,7 @@ fn find_stripping<'tcx>(
if let ExprKind::Binary(Spanned { node: BinOpKind::Sub, .. }, left, right) = end.kind; if let ExprKind::Binary(Spanned { node: BinOpKind::Sub, .. }, left, right) = end.kind;
if let Some(left_arg) = len_arg(self.cx, left); if let Some(left_arg) = len_arg(self.cx, left);
if let ExprKind::Path(left_path) = &left_arg.kind; if let ExprKind::Path(left_path) = &left_arg.kind;
if qpath_res(self.cx, left_path, left_arg.hir_id) == self.target; if self.cx.qpath_res(left_path, left_arg.hir_id) == self.target;
if eq_pattern_length(self.cx, self.pattern, right); if eq_pattern_length(self.cx, self.pattern, right);
then { then {
self.results.push(ex.span); self.results.push(ex.span);

View File

@ -1,4 +1,4 @@
use crate::utils::{match_def_path, paths, qpath_res, span_lint}; use crate::utils::{match_def_path, paths, span_lint};
use rustc_hir::{Expr, ExprKind}; use rustc_hir::{Expr, ExprKind};
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};
@ -29,7 +29,7 @@ impl<'tcx> LateLintPass<'tcx> for MemForget {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
if let ExprKind::Call(ref path_expr, ref args) = e.kind { if let ExprKind::Call(ref path_expr, ref args) = e.kind {
if let ExprKind::Path(ref qpath) = path_expr.kind { if let ExprKind::Path(ref qpath) = path_expr.kind {
if let Some(def_id) = qpath_res(cx, qpath, path_expr.hir_id).opt_def_id() { if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
if match_def_path(cx, def_id, &paths::MEM_FORGET) { if match_def_path(cx, def_id, &paths::MEM_FORGET) {
let forgot_ty = cx.typeck_results().expr_ty(&args[0]); let forgot_ty = cx.typeck_results().expr_ty(&args[0]);

View File

@ -1,4 +1,4 @@
use crate::utils::{has_drop, qpath_res, snippet_opt, span_lint, span_lint_and_sugg}; use crate::utils::{has_drop, snippet_opt, span_lint, span_lint_and_sugg};
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res}; use rustc_hir::def::{DefKind, Res};
use rustc_hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource}; use rustc_hir::{BinOpKind, BlockCheckMode, Expr, ExprKind, Stmt, StmtKind, UnsafeSource};
@ -67,7 +67,7 @@ fn has_no_effect(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
}, },
ExprKind::Call(ref callee, ref args) => { ExprKind::Call(ref callee, ref args) => {
if let ExprKind::Path(ref qpath) = callee.kind { if let ExprKind::Path(ref qpath) = callee.kind {
let res = qpath_res(cx, qpath, callee.hir_id); let res = cx.qpath_res(qpath, callee.hir_id);
match res { match res {
Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..) => { Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..) => {
!has_drop(cx, cx.typeck_results().expr_ty(expr)) !has_drop(cx, cx.typeck_results().expr_ty(expr))
@ -146,7 +146,7 @@ fn reduce_expression<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<Vec
}, },
ExprKind::Call(ref callee, ref args) => { ExprKind::Call(ref callee, ref args) => {
if let ExprKind::Path(ref qpath) = callee.kind { if let ExprKind::Path(ref qpath) = callee.kind {
let res = qpath_res(cx, qpath, callee.hir_id); let res = cx.qpath_res(qpath, callee.hir_id);
match res { match res {
Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..) Res::Def(DefKind::Struct | DefKind::Variant | DefKind::Ctor(..), ..)
if !has_drop(cx, cx.typeck_results().expr_ty(expr)) => if !has_drop(cx, cx.typeck_results().expr_ty(expr)) =>

View File

@ -18,7 +18,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{InnerSpan, Span, DUMMY_SP}; use rustc_span::{InnerSpan, Span, DUMMY_SP};
use rustc_typeck::hir_ty_to_ty; use rustc_typeck::hir_ty_to_ty;
use crate::utils::{in_constant, qpath_res, span_lint_and_then}; use crate::utils::{in_constant, span_lint_and_then};
use if_chain::if_chain; use if_chain::if_chain;
// FIXME: this is a correctness problem but there's no suitable // FIXME: this is a correctness problem but there's no suitable
@ -339,7 +339,7 @@ impl<'tcx> LateLintPass<'tcx> for NonCopyConst {
} }
// Make sure it is a const item. // Make sure it is a const item.
let item_def_id = match qpath_res(cx, qpath, expr.hir_id) { let item_def_id = match cx.qpath_res(qpath, expr.hir_id) {
Res::Def(DefKind::Const | DefKind::AssocConst, did) => did, Res::Def(DefKind::Const | DefKind::AssocConst, did) => did,
_ => return, _ => return,
}; };

View File

@ -1,4 +1,4 @@
use crate::utils::{match_def_path, match_trait_method, paths, qpath_res, span_lint}; use crate::utils::{match_def_path, match_trait_method, paths, span_lint};
use if_chain::if_chain; use if_chain::if_chain;
use rustc_hir::def::Res; use rustc_hir::def::Res;
use rustc_hir::{Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind}; use rustc_hir::{Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind};
@ -94,7 +94,7 @@ impl LateLintPass<'_> for ToStringInDisplay {
if match_trait_method(cx, expr, &paths::TO_STRING); if match_trait_method(cx, expr, &paths::TO_STRING);
if self.in_display_impl; if self.in_display_impl;
if let ExprKind::Path(ref qpath) = args[0].kind; if let ExprKind::Path(ref qpath) = args[0].kind;
if let Res::Local(hir_id) = qpath_res(cx, qpath, args[0].hir_id); if let Res::Local(hir_id) = cx.qpath_res(qpath, args[0].hir_id);
if let Some(self_hir_id) = self.self_hir_id; if let Some(self_hir_id) = self.self_hir_id;
if hir_id == self_hir_id; if hir_id == self_hir_id;
then { then {

View File

@ -34,7 +34,7 @@ use crate::utils::sugg::Sugg;
use crate::utils::{ use crate::utils::{
clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of, int_bits, is_hir_ty_cfg_dependant, clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of, int_bits, is_hir_ty_cfg_dependant,
is_type_diagnostic_item, last_path_segment, match_def_path, match_path, meets_msrv, method_chain_args, is_type_diagnostic_item, last_path_segment, match_def_path, match_path, meets_msrv, method_chain_args,
multispan_sugg, numeric_literal::NumericLiteral, qpath_res, reindent_multiline, sext, snippet, snippet_opt, multispan_sugg, numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt,
snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg,
span_lint_and_then, unsext, span_lint_and_then, unsext,
}; };
@ -298,7 +298,7 @@ fn match_type_parameter(cx: &LateContext<'_>, qpath: &QPath<'_>, path: &[&str])
_ => None, _ => None,
}); });
if let TyKind::Path(ref qpath) = ty.kind; if let TyKind::Path(ref qpath) = ty.kind;
if let Some(did) = qpath_res(cx, qpath, ty.hir_id).opt_def_id(); if let Some(did) = cx.qpath_res(qpath, ty.hir_id).opt_def_id();
if match_def_path(cx, did, path); if match_def_path(cx, did, path);
then { then {
return Some(ty.span); return Some(ty.span);
@ -365,7 +365,7 @@ impl Types {
match hir_ty.kind { match hir_ty.kind {
TyKind::Path(ref qpath) if !is_local => { TyKind::Path(ref qpath) if !is_local => {
let hir_id = hir_ty.hir_id; let hir_id = hir_ty.hir_id;
let res = qpath_res(cx, qpath, hir_id); let res = cx.qpath_res(qpath, hir_id);
if let Some(def_id) = res.opt_def_id() { if let Some(def_id) = res.opt_def_id() {
if Some(def_id) == cx.tcx.lang_items().owned_box() { if Some(def_id) == cx.tcx.lang_items().owned_box() {
if let Some(span) = match_borrows_parameter(cx, qpath) { if let Some(span) = match_borrows_parameter(cx, qpath) {
@ -535,7 +535,7 @@ impl Types {
}); });
// ty is now _ at this point // ty is now _ at this point
if let TyKind::Path(ref ty_qpath) = ty.kind; if let TyKind::Path(ref ty_qpath) = ty.kind;
let res = qpath_res(cx, ty_qpath, ty.hir_id); let res = cx.qpath_res(ty_qpath, ty.hir_id);
if let Some(def_id) = res.opt_def_id(); if let Some(def_id) = res.opt_def_id();
if Some(def_id) == cx.tcx.lang_items().owned_box(); if Some(def_id) == cx.tcx.lang_items().owned_box();
// At this point, we know ty is Box<T>, now get T // At this point, we know ty is Box<T>, now get T
@ -652,7 +652,7 @@ impl Types {
match mut_ty.ty.kind { match mut_ty.ty.kind {
TyKind::Path(ref qpath) => { TyKind::Path(ref qpath) => {
let hir_id = mut_ty.ty.hir_id; let hir_id = mut_ty.ty.hir_id;
let def = qpath_res(cx, qpath, hir_id); let def = cx.qpath_res(qpath, hir_id);
if_chain! { if_chain! {
if let Some(def_id) = def.opt_def_id(); if let Some(def_id) = def.opt_def_id();
if Some(def_id) == cx.tcx.lang_items().owned_box(); if Some(def_id) == cx.tcx.lang_items().owned_box();
@ -739,7 +739,7 @@ fn is_any_trait(t: &hir::Ty<'_>) -> bool {
fn get_bounds_if_impl_trait<'tcx>(cx: &LateContext<'tcx>, qpath: &QPath<'_>, id: HirId) -> Option<GenericBounds<'tcx>> { fn get_bounds_if_impl_trait<'tcx>(cx: &LateContext<'tcx>, qpath: &QPath<'_>, id: HirId) -> Option<GenericBounds<'tcx>> {
if_chain! { if_chain! {
if let Some(did) = qpath_res(cx, qpath, id).opt_def_id(); if let Some(did) = cx.qpath_res(qpath, id).opt_def_id();
if let Some(Node::GenericParam(generic_param)) = cx.tcx.hir().get_if_local(did); if let Some(Node::GenericParam(generic_param)) = cx.tcx.hir().get_if_local(did);
if let GenericParamKind::Type { synthetic, .. } = generic_param.kind; if let GenericParamKind::Type { synthetic, .. } = generic_param.kind;
if synthetic == Some(SyntheticTyParamKind::ImplTrait); if synthetic == Some(SyntheticTyParamKind::ImplTrait);

View File

@ -1,7 +1,7 @@
use crate::consts::{constant_simple, Constant}; use crate::consts::{constant_simple, Constant};
use crate::utils::{ use crate::utils::{
is_expn_of, match_def_path, match_qpath, match_type, method_calls, path_to_res, paths, qpath_res, run_lints, is_expn_of, match_def_path, match_qpath, match_type, method_calls, path_to_res, paths, run_lints, snippet,
snippet, span_lint, span_lint_and_help, span_lint_and_sugg, SpanlessEq, span_lint, span_lint_and_help, span_lint_and_sugg, SpanlessEq,
}; };
use if_chain::if_chain; use if_chain::if_chain;
use rustc_ast::ast::{Crate as AstCrate, ItemKind, LitKind, NodeId}; use rustc_ast::ast::{Crate as AstCrate, ItemKind, LitKind, NodeId};
@ -787,7 +787,7 @@ fn path_to_matched_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<Ve
match &expr.kind { match &expr.kind {
ExprKind::AddrOf(.., expr) => return path_to_matched_type(cx, expr), ExprKind::AddrOf(.., expr) => return path_to_matched_type(cx, expr),
ExprKind::Path(qpath) => match qpath_res(cx, qpath, expr.hir_id) { ExprKind::Path(qpath) => match cx.qpath_res(qpath, expr.hir_id) {
Res::Local(hir_id) => { Res::Local(hir_id) => {
let parent_id = cx.tcx.hir().get_parent_node(hir_id); let parent_id = cx.tcx.hir().get_parent_node(hir_id);
if let Some(Node::Local(local)) = cx.tcx.hir().find(parent_id) { if let Some(Node::Local(local)) = cx.tcx.hir().find(parent_id) {

View File

@ -370,19 +370,6 @@ 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(..) | hir::QPath::LangItem(..) => {
if cx.tcx.has_typeck_results(id.owner.to_def_id()) {
cx.tcx.typeck(id.owner).qpath_res(qpath, id)
} else {
Res::Err
}
},
}
}
/// Convenience function to get the `DefId` of a trait by path. /// Convenience function to get the `DefId` of a trait by path.
/// It could be a trait or trait alias. /// It could be a trait or trait alias.
pub fn get_trait_def_id(cx: &LateContext<'_>, path: &[&str]) -> Option<DefId> { pub fn get_trait_def_id(cx: &LateContext<'_>, path: &[&str]) -> Option<DefId> {