Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clippy_lints/src/manual_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ fn is_arms_disjointed(cx: &LateContext<'_>, arm1: &Arm<'_>, arm2: &Arm<'_>) -> b

/// Returns `true` if the given pattern is a variant of an enum.
pub fn is_enum_variant(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
let path = match pat.kind {
let path: (&QPath<'_>, _) = match pat.kind {
PatKind::Struct(ref qpath, fields, _)
if fields
.iter()
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/matches/match_wild_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
// this is incorrect, as we should really check whether each variant is exhaustively
// covered by the set of guards that cover it, but that's really hard to do.
recurse_or_patterns(arm.pat, |pat| {
let path = match &peel_hir_pat_refs(pat).0.kind {
let path: &QPath<'_> = match &peel_hir_pat_refs(pat).0.kind {
PatKind::Expr(PatExpr {
hir_id,
kind: PatExprKind::Path(path),
Expand Down
6 changes: 4 additions & 2 deletions clippy_lints/src/matches/rest_pat_in_fully_bound_struct.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_hir::{Pat, PatKind, QPath};
use clippy_utils::resolved_path;
use rustc_hir::{Pat, PatKind};
use rustc_lint::LateContext;
use rustc_middle::ty;

use super::REST_PAT_IN_FULLY_BOUND_STRUCTS;

pub(crate) fn check(cx: &LateContext<'_>, pat: &Pat<'_>) {
if !pat.span.from_expansion()
&& let PatKind::Struct(QPath::Resolved(_, path), fields, Some(dotdot)) = pat.kind
&& let PatKind::Struct(ref qpath, fields, Some(dotdot)) = pat.kind
&& let Some(path) = resolved_path(qpath)
&& let Some(def_id) = path.res.opt_def_id()
&& let ty = cx.tcx.type_of(def_id).instantiate_identity().skip_norm_wip()
&& let ty::Adt(def, _) = ty.kind()
Expand Down
7 changes: 4 additions & 3 deletions clippy_lints/src/methods/string_from_utf8_as_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use clippy_utils::{method_calls, sym};
use rustc_ast::BorrowKind;
use rustc_errors::Applicability;
use rustc_hir::attrs::lang_items::LangItem;
use rustc_hir::{Expr, ExprKind};
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_lint::LateContext;

pub(super) fn check_call(cx: &LateContext<'_>, expr: &Expr<'_>, fun: &Expr<'_>, args: &[Expr<'_>]) {
let is_range = |qpath: &QPath<'_>| cx.tcx.qpath_is_lang_item(*qpath, LangItem::Range);
// Find `std::str::converts::from_utf8` or `std::primitive::str::from_utf8`

if let [bytes_arg] = args && let Some(sym::str_from_utf8 | sym::str_inherent_from_utf8) =
Expand All @@ -24,8 +25,8 @@ pub(super) fn check_call(cx: &LateContext<'_>, expr: &Expr<'_>, fun: &Expr<'_>,
&& expressions[0].1.is_empty()

// Check for slicer
&& let ExprKind::Struct(&qpath, _, _) = right.kind
&& cx.tcx.qpath_is_lang_item(qpath, LangItem::Range)
&& let ExprKind::Struct(qpath, _, _) = right.kind
&& is_range(qpath)
{
let mut applicability = Applicability::MachineApplicable;
let string_expression = &expressions[0].0;
Expand Down
10 changes: 5 additions & 5 deletions clippy_lints/src/unneeded_struct_pattern.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_from_proc_macro;
use clippy_utils::{is_from_proc_macro, resolved_path};
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Pat, PatKind, QPath};
use rustc_hir::{Pat, PatKind};
use rustc_lint::{LateContext, LateLintPass, declare_lint_pass};

declare_clippy_lint! {
Expand Down Expand Up @@ -42,8 +42,8 @@ declare_lint_pass!(UnneededStructPattern => [UNNEEDED_STRUCT_PATTERN]);
impl LateLintPass<'_> for UnneededStructPattern {
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
if !pat.span.from_expansion()
&& let PatKind::Struct(path, [], _) = &pat.kind
&& let QPath::Resolved(_, path) = path
&& let PatKind::Struct(ref qpath, [], _) = pat.kind
&& let Some(path) = resolved_path(qpath)
&& let Res::Def(DefKind::Variant, did) = path.res
{
let enum_did = cx.tcx.parent(did);
Expand All @@ -55,7 +55,7 @@ impl LateLintPass<'_> for UnneededStructPattern {
return;
}

if is_from_proc_macro(cx, *path) {
if is_from_proc_macro(cx, path) {
return;
}

Expand Down
17 changes: 11 additions & 6 deletions clippy_lints/src/use_self.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_from_proc_macro;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::ty::{same_type_modulo_regions, ty_from_hir_ty};
use clippy_utils::{is_from_proc_macro, resolved_path};
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::Applicability;
use rustc_hir::def::{CtorOf, DefKind, Res};
Expand Down Expand Up @@ -235,7 +235,7 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
&& self.msrv.meets(cx, msrvs::TYPE_ALIAS_ENUM_VARIANTS)
{
match expr.kind {
ExprKind::Struct(QPath::Resolved(_, path), ..) => check_path(cx, path),
ExprKind::Struct(qpath, ..) if let Some(path) = resolved_path(qpath) => check_path(cx, path),
ExprKind::Call(fun, _) => {
if let ExprKind::Path(QPath::Resolved(_, path)) = fun.kind {
check_path(cx, path);
Expand All @@ -250,10 +250,15 @@ impl<'tcx> LateLintPass<'tcx> for UseSelf {
fn check_pat(&mut self, cx: &LateContext<'_>, pat: &Pat<'_>) {
if !pat.span.from_expansion()
&& let Some(&StackItem::Check { impl_id, .. }) = self.stack.last()
// get the path from the pattern
&& let PatKind::Expr(&PatExpr { kind: PatExprKind::Path(QPath::Resolved(_, path)), .. })
| PatKind::TupleStruct(QPath::Resolved(_, path), _, _)
| PatKind::Struct(QPath::Resolved(_, path), _, _) = pat.kind
&& let Some(path) = match &pat.kind {
PatKind::Expr(PatExpr {
kind: PatExprKind::Path(QPath::Resolved(_, path)),
..
})
| PatKind::TupleStruct(QPath::Resolved(_, path), _, _) => Some(*path),
PatKind::Struct(qpath, _, _) => resolved_path(qpath),
_ => None,
}
&& cx.typeck_results().pat_ty(pat) == cx.tcx.type_of(impl_id).instantiate_identity().skip_norm_wip()
&& self.msrv.meets(cx, msrvs::TYPE_ALIAS_ENUM_VARIANTS)
{
Expand Down
6 changes: 4 additions & 2 deletions clippy_lints/src/utils/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,8 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
bind!(self, container, fields);
kind!("OffsetOf({container}, {fields})");
},
ExprKind::Struct(qpath, fields, base) => {
ExprKind::Struct(path, fields, base) => {
let qpath: &QPath<'_> = path;
bind!(self, qpath, fields);
let base = OptionPat::new(match base {
StructTailExpr::Base(base) => Some(self.bind("base", base)),
Expand Down Expand Up @@ -778,7 +779,8 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
self.ident(name);
sub.if_some(|p| self.pat(p));
},
PatKind::Struct(ref qpath, fields, etc) => {
PatKind::Struct(ref path, fields, etc) => {
let qpath: &QPath<'_> = path;
let ignore = etc.is_some();
bind!(self, qpath, fields);
kind!("Struct(ref {qpath}, {fields}, {ignore})");
Expand Down
5 changes: 3 additions & 2 deletions clippy_utils/src/higher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,15 @@ impl<'a> Range<'a> {
/// Higher a `hir` range to something similar to `ast::ExprKind::Range`.
pub fn hir(cx: &LateContext<'_>, expr: &'a Expr<'_>) -> Option<Range<'a>> {
let span = expr.range_span()?;
let lang_item = |path: &QPath<'_>| cx.tcx.qpath_lang_item(*path);
let (ty, start, end) = match expr.kind {
ExprKind::Call(path, [arg1, arg2])
if let ExprKind::Path(qpath) = path.kind
&& cx.tcx.qpath_is_lang_item(qpath, LangItem::RangeInclusiveNew) =>
&& lang_item(&qpath) == Some(LangItem::RangeInclusiveNew) =>
{
(RangeTy::OpsInclusive, Some(arg1), Some(arg2))
},
ExprKind::Struct(&qpath, fields, StructTailExpr::None) => match (cx.tcx.qpath_lang_item(qpath)?, fields) {
ExprKind::Struct(qpath, fields, StructTailExpr::None) => match (lang_item(qpath)?, fields) {
(LangItem::RangeFull, []) => (RangeTy::OpsFull, None, None),
(LangItem::RangeFrom, [start]) if start.ident.name == sym::start => {
(RangeTy::OpsFrom, Some(start.expr), None)
Expand Down
8 changes: 8 additions & 0 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,14 @@ pub fn is_def_id_trait_method(cx: &LateContext<'_>, def_id: LocalDefId) -> bool
}
}

/// Returns the resolved path, if name resolution did not defer it to type checking.
pub fn resolved_path<'tcx>(qpath: &QPath<'tcx>) -> Option<&'tcx Path<'tcx>> {
match *qpath {
QPath::Resolved(_, path) => Some(path),
QPath::TypeRelative(..) => None,
}
}

pub fn last_path_segment<'tcx>(path: &QPath<'tcx>) -> &'tcx PathSegment<'tcx> {
match *path {
QPath::Resolved(_, path) => path.segments.last().expect("A path must have at least one segment"),
Expand Down
Loading