mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-13 00:17:13 +00:00
Auto merge of #6881 - flip1995:rustup, r=flip1995
Rustup changelog: none
This commit is contained in:
commit
99afc6e6ac
43 changed files with 173 additions and 138 deletions
|
@ -276,14 +276,15 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
|
|||
}
|
||||
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
if is_relevant_item(cx, item) {
|
||||
check_attrs(cx, item.span, item.ident.name, &item.attrs)
|
||||
check_attrs(cx, item.span, item.ident.name, attrs)
|
||||
}
|
||||
match item.kind {
|
||||
ItemKind::ExternCrate(..) | ItemKind::Use(..) => {
|
||||
let skip_unused_imports = item.attrs.iter().any(|attr| attr.has_name(sym::macro_use));
|
||||
let skip_unused_imports = attrs.iter().any(|attr| attr.has_name(sym::macro_use));
|
||||
|
||||
for attr in item.attrs {
|
||||
for attr in attrs {
|
||||
if in_external_macro(cx.sess(), attr.span) {
|
||||
return;
|
||||
}
|
||||
|
@ -353,13 +354,13 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
|
|||
|
||||
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx ImplItem<'_>) {
|
||||
if is_relevant_impl(cx, item) {
|
||||
check_attrs(cx, item.span, item.ident.name, &item.attrs)
|
||||
check_attrs(cx, item.span, item.ident.name, cx.tcx.hir().attrs(item.hir_id()))
|
||||
}
|
||||
}
|
||||
|
||||
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
|
||||
if is_relevant_trait(cx, item) {
|
||||
check_attrs(cx, item.span, item.ident.name, &item.attrs)
|
||||
check_attrs(cx, item.span, item.ident.name, cx.tcx.hir().attrs(item.hir_id()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,8 +76,8 @@ impl CognitiveComplexity {
|
|||
|
||||
if rust_cc > self.limit.limit() {
|
||||
let fn_span = match kind {
|
||||
FnKind::ItemFn(ident, _, _, _, _) | FnKind::Method(ident, _, _, _) => ident.span,
|
||||
FnKind::Closure(_) => {
|
||||
FnKind::ItemFn(ident, _, _, _) | FnKind::Method(ident, _, _) => ident.span,
|
||||
FnKind::Closure => {
|
||||
let header_span = body_span.with_hi(decl.output.span().lo());
|
||||
let pos = snippet_opt(cx, header_span).and_then(|snip| {
|
||||
let low_offset = snip.find('|')?;
|
||||
|
|
|
@ -170,7 +170,8 @@ impl<'tcx> LateLintPass<'tcx> for Derive {
|
|||
}) = item.kind
|
||||
{
|
||||
let ty = cx.tcx.type_of(item.def_id);
|
||||
let is_automatically_derived = is_automatically_derived(&*item.attrs);
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
let is_automatically_derived = is_automatically_derived(attrs);
|
||||
|
||||
check_hash_peq(cx, item.span, trait_ref, ty, is_automatically_derived);
|
||||
check_ord_partial_ord(cx, item.span, trait_ref, ty, is_automatically_derived);
|
||||
|
|
|
@ -208,12 +208,14 @@ impl_lint_pass!(DocMarkdown =>
|
|||
);
|
||||
|
||||
impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
|
||||
fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
|
||||
check_attrs(cx, &self.valid_idents, &krate.item.attrs);
|
||||
fn check_crate(&mut self, cx: &LateContext<'tcx>, _: &'tcx hir::Crate<'_>) {
|
||||
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
|
||||
check_attrs(cx, &self.valid_idents, attrs);
|
||||
}
|
||||
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
|
||||
let headers = check_attrs(cx, &self.valid_idents, &item.attrs);
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
let headers = check_attrs(cx, &self.valid_idents, attrs);
|
||||
match item.kind {
|
||||
hir::ItemKind::Fn(ref sig, _, body_id) => {
|
||||
if !(is_entrypoint_fn(cx, item.def_id.to_def_id()) || in_external_macro(cx.tcx.sess, item.span)) {
|
||||
|
@ -249,7 +251,8 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
|
|||
}
|
||||
|
||||
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
|
||||
let headers = check_attrs(cx, &self.valid_idents, &item.attrs);
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
let headers = check_attrs(cx, &self.valid_idents, attrs);
|
||||
if let hir::TraitItemKind::Fn(ref sig, ..) = item.kind {
|
||||
if !in_external_macro(cx.tcx.sess, item.span) {
|
||||
lint_for_missing_headers(cx, item.hir_id(), item.span, sig, headers, None, None);
|
||||
|
@ -258,7 +261,8 @@ impl<'tcx> LateLintPass<'tcx> for DocMarkdown {
|
|||
}
|
||||
|
||||
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
|
||||
let headers = check_attrs(cx, &self.valid_idents, &item.attrs);
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
let headers = check_attrs(cx, &self.valid_idents, attrs);
|
||||
if self.in_trait_impl || in_external_macro(cx.tcx.sess, item.span) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ use rustc_lint::{LateContext, LateLintPass};
|
|||
use rustc_middle::hir::map::Map;
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::source_map::Span;
|
||||
use rustc_span::sym;
|
||||
|
||||
declare_clippy_lint! {
|
||||
/// **What it does:** Checks for uses of `contains_key` + `insert` on `HashMap`
|
||||
|
@ -111,7 +112,7 @@ fn check_cond<'a>(cx: &LateContext<'_>, check: &'a Expr<'a>) -> Option<(&'static
|
|||
return if match_type(cx, obj_ty, &paths::BTREEMAP) {
|
||||
Some(("BTreeMap", map, key))
|
||||
}
|
||||
else if is_type_diagnostic_item(cx, obj_ty, sym!(hashmap_type)) {
|
||||
else if is_type_diagnostic_item(cx, obj_ty, sym::hashmap_type) {
|
||||
Some(("HashMap", map, key))
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -73,7 +73,8 @@ impl LateLintPass<'_> for ExhaustiveItems {
|
|||
if_chain! {
|
||||
if let ItemKind::Enum(..) | ItemKind::Struct(..) = item.kind;
|
||||
if cx.access_levels.is_exported(item.hir_id());
|
||||
if !item.attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
if !attrs.iter().any(|a| a.has_name(sym::non_exhaustive));
|
||||
then {
|
||||
let (lint, msg) = if let ItemKind::Struct(ref v, ..) = item.kind {
|
||||
if v.fields().iter().any(|f| !f.vis.node.is_pub()) {
|
||||
|
|
|
@ -251,9 +251,9 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
|
|||
hir_id: hir::HirId,
|
||||
) {
|
||||
let unsafety = match kind {
|
||||
intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }, _, _) => unsafety,
|
||||
intravisit::FnKind::Method(_, sig, _, _) => sig.header.unsafety,
|
||||
intravisit::FnKind::Closure(_) => return,
|
||||
intravisit::FnKind::ItemFn(_, _, hir::FnHeader { unsafety, .. }, _) => unsafety,
|
||||
intravisit::FnKind::Method(_, sig, _) => sig.header.unsafety,
|
||||
intravisit::FnKind::Closure => return,
|
||||
};
|
||||
|
||||
// don't warn for implementations, it's not their fault
|
||||
|
@ -267,9 +267,8 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
|
|||
..
|
||||
},
|
||||
_,
|
||||
_,
|
||||
)
|
||||
| intravisit::FnKind::ItemFn(_, _, hir::FnHeader { abi: Abi::Rust, .. }, _, _) => {
|
||||
| intravisit::FnKind::ItemFn(_, _, hir::FnHeader { abi: Abi::Rust, .. }, _) => {
|
||||
self.check_arg_number(cx, decl, span.with_hi(decl.output.span().hi()))
|
||||
},
|
||||
_ => {},
|
||||
|
@ -281,7 +280,8 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
|
|||
}
|
||||
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
|
||||
let attr = must_use_attr(&item.attrs);
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
let attr = must_use_attr(attrs);
|
||||
if let hir::ItemKind::Fn(ref sig, ref _generics, ref body_id) = item.kind {
|
||||
let is_public = cx.access_levels.is_exported(item.hir_id());
|
||||
let fn_header_span = item.span.with_hi(sig.decl.output.span().hi());
|
||||
|
@ -292,7 +292,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
|
|||
check_needless_must_use(cx, &sig.decl, item.hir_id(), item.span, fn_header_span, attr);
|
||||
return;
|
||||
}
|
||||
if is_public && !is_proc_macro(cx.sess(), &item.attrs) && attr_by_name(&item.attrs, "no_mangle").is_none() {
|
||||
if is_public && !is_proc_macro(cx.sess(), attrs) && attr_by_name(attrs, "no_mangle").is_none() {
|
||||
check_must_use_candidate(
|
||||
cx,
|
||||
&sig.decl,
|
||||
|
@ -313,12 +313,11 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
|
|||
if is_public && trait_ref_of_method(cx, item.hir_id()).is_none() {
|
||||
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
|
||||
}
|
||||
let attr = must_use_attr(&item.attrs);
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
let attr = must_use_attr(attrs);
|
||||
if let Some(attr) = attr {
|
||||
check_needless_must_use(cx, &sig.decl, item.hir_id(), item.span, fn_header_span, attr);
|
||||
} else if is_public
|
||||
&& !is_proc_macro(cx.sess(), &item.attrs)
|
||||
&& trait_ref_of_method(cx, item.hir_id()).is_none()
|
||||
} else if is_public && !is_proc_macro(cx.sess(), attrs) && trait_ref_of_method(cx, item.hir_id()).is_none()
|
||||
{
|
||||
check_must_use_candidate(
|
||||
cx,
|
||||
|
@ -345,7 +344,8 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
|
|||
check_result_unit_err(cx, &sig.decl, item.span, fn_header_span);
|
||||
}
|
||||
|
||||
let attr = must_use_attr(&item.attrs);
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
let attr = must_use_attr(attrs);
|
||||
if let Some(attr) = attr {
|
||||
check_needless_must_use(cx, &sig.decl, item.hir_id(), item.span, fn_header_span, attr);
|
||||
}
|
||||
|
@ -353,7 +353,7 @@ impl<'tcx> LateLintPass<'tcx> for Functions {
|
|||
let body = cx.tcx.hir().body(eid);
|
||||
Self::check_raw_ptr(cx, sig.header.unsafety, &sig.decl, body, item.hir_id());
|
||||
|
||||
if attr.is_none() && is_public && !is_proc_macro(cx.sess(), &item.attrs) {
|
||||
if attr.is_none() && is_public && !is_proc_macro(cx.sess(), attrs) {
|
||||
check_must_use_candidate(
|
||||
cx,
|
||||
&sig.decl,
|
||||
|
|
|
@ -58,7 +58,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend {
|
|||
_: Span,
|
||||
hir_id: HirId,
|
||||
) {
|
||||
if let FnKind::Closure(_) = kind {
|
||||
if let FnKind::Closure = kind {
|
||||
return;
|
||||
}
|
||||
let ret_ty = utils::return_ty(cx, hir_id);
|
||||
|
|
|
@ -34,7 +34,8 @@ declare_lint_pass!(InlineFnWithoutBody => [INLINE_FN_WITHOUT_BODY]);
|
|||
impl<'tcx> LateLintPass<'tcx> for InlineFnWithoutBody {
|
||||
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) {
|
||||
if let TraitItemKind::Fn(_, TraitFn::Required(_)) = item.kind {
|
||||
check_attrs(cx, item.ident.name, &item.attrs);
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
check_attrs(cx, item.ident.name, attrs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ use rustc_lint::LateContext;
|
|||
pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) {
|
||||
if loop_block.stmts.is_empty() && loop_block.expr.is_none() && !is_in_panic_handler(cx, expr) {
|
||||
let msg = "empty `loop {}` wastes CPU cycles";
|
||||
let help = if is_no_std_crate(cx.tcx.hir().krate()) {
|
||||
let help = if is_no_std_crate(cx) {
|
||||
"you should either use `panic!()` or add a call pausing or sleeping the thread to the loop body"
|
||||
} else {
|
||||
"you should either use `panic!()` or add `std::thread::sleep(..);` to the loop body"
|
||||
|
|
|
@ -4,7 +4,7 @@ use rustc_errors::Applicability;
|
|||
use rustc_hir::{Expr, Mutability};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty::{self, Ty, TyS};
|
||||
use rustc_span::symbol::sym;
|
||||
use rustc_span::sym;
|
||||
|
||||
use crate::utils::{is_type_diagnostic_item, match_type, paths};
|
||||
|
||||
|
@ -55,9 +55,9 @@ fn is_ref_iterable_type(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
|
|||
is_iterable_array(ty, cx) ||
|
||||
is_type_diagnostic_item(cx, ty, sym::vec_type) ||
|
||||
match_type(cx, ty, &paths::LINKED_LIST) ||
|
||||
is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) ||
|
||||
is_type_diagnostic_item(cx, ty, sym!(hashset_type)) ||
|
||||
is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) ||
|
||||
is_type_diagnostic_item(cx, ty, sym::hashmap_type) ||
|
||||
is_type_diagnostic_item(cx, ty, sym::hashset_type) ||
|
||||
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
|
||||
match_type(cx, ty, &paths::BINARY_HEAP) ||
|
||||
match_type(cx, ty, &paths::BTREEMAP) ||
|
||||
match_type(cx, ty, &paths::BTREESET)
|
||||
|
|
|
@ -4,6 +4,7 @@ use crate::utils::{is_type_diagnostic_item, match_type, multispan_sugg, paths, s
|
|||
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Pat, PatKind};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::ty;
|
||||
use rustc_span::sym;
|
||||
|
||||
/// Checks for the `FOR_KV_MAP` lint.
|
||||
pub(super) fn check<'tcx>(
|
||||
|
@ -35,7 +36,7 @@ pub(super) fn check<'tcx>(
|
|||
_ => arg,
|
||||
};
|
||||
|
||||
if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) || match_type(cx, ty, &paths::BTREEMAP) {
|
||||
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) || match_type(cx, ty, &paths::BTREEMAP) {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
FOR_KV_MAP,
|
||||
|
|
|
@ -203,7 +203,7 @@ struct MinifyingSugg<'a>(Sugg<'a>);
|
|||
|
||||
impl<'a> MinifyingSugg<'a> {
|
||||
fn as_str(&self) -> &str {
|
||||
let Sugg::NonParen(s) | Sugg::MaybeParen(s) | Sugg::BinOp(_, s) = &self.0;
|
||||
let (Sugg::NonParen(s) | Sugg::MaybeParen(s) | Sugg::BinOp(_, s)) = &self.0;
|
||||
s.as_ref()
|
||||
}
|
||||
|
||||
|
@ -328,7 +328,7 @@ fn is_slice_like<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'_>) -> bool {
|
|||
_ => false,
|
||||
};
|
||||
|
||||
is_slice || is_type_diagnostic_item(cx, ty, sym::vec_type) || is_type_diagnostic_item(cx, ty, sym!(vecdeque_type))
|
||||
is_slice || is_type_diagnostic_item(cx, ty, sym::vec_type) || is_type_diagnostic_item(cx, ty, sym::vecdeque_type)
|
||||
}
|
||||
|
||||
fn fetch_cloned_expr<'tcx>(expr: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
|
||||
|
|
|
@ -29,9 +29,9 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
|
|||
then {
|
||||
let ty = cx.typeck_results().node_type(ty.hir_id);
|
||||
if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
|
||||
is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) ||
|
||||
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
|
||||
match_type(cx, ty, &paths::BTREEMAP) ||
|
||||
is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) {
|
||||
is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
|
||||
if method.ident.name == sym!(len) {
|
||||
let span = shorten_needless_collect_span(expr);
|
||||
span_lint_and_sugg(
|
||||
|
@ -99,7 +99,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
|
|||
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
|
||||
if let ty = cx.typeck_results().node_type(ty.hir_id);
|
||||
if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
|
||||
is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) ||
|
||||
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
|
||||
match_type(cx, ty, &paths::LINKED_LIST);
|
||||
if let Some(iter_calls) = detect_iter_and_into_iters(block, *ident);
|
||||
if iter_calls.len() == 1;
|
||||
|
|
|
@ -107,8 +107,8 @@ impl<'tcx> LateLintPass<'tcx> for MacroUseImports {
|
|||
if_chain! {
|
||||
if cx.sess().opts.edition >= Edition::Edition2018;
|
||||
if let hir::ItemKind::Use(path, _kind) = &item.kind;
|
||||
if let Some(mac_attr) = item
|
||||
.attrs
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
if let Some(mac_attr) = attrs
|
||||
.iter()
|
||||
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
|
||||
if let Res::Def(DefKind::Mod, id) = path.res;
|
||||
|
|
|
@ -32,8 +32,8 @@ pub struct MainRecursion {
|
|||
impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);
|
||||
|
||||
impl LateLintPass<'_> for MainRecursion {
|
||||
fn check_crate(&mut self, _: &LateContext<'_>, krate: &Crate<'_>) {
|
||||
self.has_no_std_attr = is_no_std_crate(krate);
|
||||
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
|
||||
self.has_no_std_attr = is_no_std_crate(cx);
|
||||
}
|
||||
|
||||
fn check_expr_post(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
||||
|
|
|
@ -1207,11 +1207,11 @@ fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr
|
|||
if b0 != b1;
|
||||
let if_guard = &b0_arms[0].guard;
|
||||
if if_guard.is_none() || b0_arms.len() == 1;
|
||||
if b0_arms[0].attrs.is_empty();
|
||||
if cx.tcx.hir().attrs(b0_arms[0].hir_id).is_empty();
|
||||
if b0_arms[1..].iter()
|
||||
.all(|arm| {
|
||||
find_bool_lit(&arm.body.kind, desugared).map_or(false, |b| b == b0) &&
|
||||
arm.guard.is_none() && arm.attrs.is_empty()
|
||||
arm.guard.is_none() && cx.tcx.hir().attrs(arm.hir_id).is_empty()
|
||||
});
|
||||
then {
|
||||
// The suggestion may be incorrect, because some arms can have `cfg` attributes
|
||||
|
|
|
@ -14,11 +14,11 @@ pub(crate) fn lints<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, iter_args: &'
|
|||
"slice"
|
||||
} else if is_type_diagnostic_item(cx, ty, sym::vec_type) {
|
||||
"Vec"
|
||||
} else if is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) {
|
||||
} else if is_type_diagnostic_item(cx, ty, sym::vecdeque_type) {
|
||||
"VecDeque"
|
||||
} else if is_type_diagnostic_item(cx, ty, sym!(hashset_type)) {
|
||||
} else if is_type_diagnostic_item(cx, ty, sym::hashset_type) {
|
||||
"HashSet"
|
||||
} else if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) {
|
||||
} else if is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
|
||||
"HashMap"
|
||||
} else if match_type(cx, ty, &paths::BTREEMAP) {
|
||||
"BTreeMap"
|
||||
|
|
|
@ -26,7 +26,7 @@ use rustc_middle::ty::{self, TraitRef, Ty, TyS};
|
|||
use rustc_semver::RustcVersion;
|
||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
||||
use rustc_span::source_map::Span;
|
||||
use rustc_span::symbol::{sym, SymbolStr};
|
||||
use rustc_span::symbol::{sym, Symbol, SymbolStr};
|
||||
use rustc_typeck::hir_ty_to_ty;
|
||||
|
||||
use crate::consts::{constant, Constant};
|
||||
|
@ -2675,7 +2675,7 @@ fn lint_iter_nth<'tcx>(
|
|||
"slice"
|
||||
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) {
|
||||
"Vec"
|
||||
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym!(vecdeque_type)) {
|
||||
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vecdeque_type) {
|
||||
"VecDeque"
|
||||
} else {
|
||||
let nth_args = nth_and_iter_args[0];
|
||||
|
@ -2729,10 +2729,10 @@ fn lint_get_unwrap<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, get_args:
|
|||
} else if is_type_diagnostic_item(cx, expr_ty, sym::vec_type) {
|
||||
needs_ref = get_args_str.parse::<usize>().is_ok();
|
||||
"Vec"
|
||||
} else if is_type_diagnostic_item(cx, expr_ty, sym!(vecdeque_type)) {
|
||||
} else if is_type_diagnostic_item(cx, expr_ty, sym::vecdeque_type) {
|
||||
needs_ref = get_args_str.parse::<usize>().is_ok();
|
||||
"VecDeque"
|
||||
} else if !is_mut && is_type_diagnostic_item(cx, expr_ty, sym!(hashmap_type)) {
|
||||
} else if !is_mut && is_type_diagnostic_item(cx, expr_ty, sym::hashmap_type) {
|
||||
needs_ref = true;
|
||||
"HashMap"
|
||||
} else if !is_mut && match_type(cx, expr_ty, &paths::BTREEMAP) {
|
||||
|
@ -3696,7 +3696,7 @@ fn lint_asref(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_re
|
|||
}
|
||||
}
|
||||
|
||||
fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(&'static str, &'static str)> {
|
||||
fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(Symbol, &'static str)> {
|
||||
has_iter_method(cx, self_ref_ty).map(|ty_name| {
|
||||
let mutbl = match self_ref_ty.kind() {
|
||||
ty::Ref(_, _, mutbl) => mutbl,
|
||||
|
|
|
@ -279,7 +279,7 @@ impl<'tcx> LateLintPass<'tcx> for MiscLints {
|
|||
span: Span,
|
||||
_: HirId,
|
||||
) {
|
||||
if let FnKind::Closure(_) = k {
|
||||
if let FnKind::Closure = k {
|
||||
// Does not apply to closures
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -133,7 +133,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
|
|||
return;
|
||||
}
|
||||
},
|
||||
FnKind::Closure(..) => return,
|
||||
FnKind::Closure => return,
|
||||
}
|
||||
|
||||
let mir = cx.tcx.optimized_mir(def_id);
|
||||
|
|
|
@ -127,7 +127,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
|
|||
}
|
||||
|
||||
fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
|
||||
self.check_missing_docs_attrs(cx, &krate.item.attrs, krate.item.span, "the", "crate");
|
||||
let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
|
||||
self.check_missing_docs_attrs(cx, attrs, krate.item.span, "the", "crate");
|
||||
}
|
||||
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
|
||||
|
@ -160,13 +161,15 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
|
|||
|
||||
let (article, desc) = cx.tcx.article_and_description(it.def_id.to_def_id());
|
||||
|
||||
self.check_missing_docs_attrs(cx, &it.attrs, it.span, article, desc);
|
||||
let attrs = cx.tcx.hir().attrs(it.hir_id());
|
||||
self.check_missing_docs_attrs(cx, attrs, it.span, article, desc);
|
||||
}
|
||||
|
||||
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::TraitItem<'_>) {
|
||||
let (article, desc) = cx.tcx.article_and_description(trait_item.def_id.to_def_id());
|
||||
|
||||
self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, article, desc);
|
||||
let attrs = cx.tcx.hir().attrs(trait_item.hir_id());
|
||||
self.check_missing_docs_attrs(cx, attrs, trait_item.span, article, desc);
|
||||
}
|
||||
|
||||
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
|
||||
|
@ -181,16 +184,19 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
|
|||
}
|
||||
|
||||
let (article, desc) = cx.tcx.article_and_description(impl_item.def_id.to_def_id());
|
||||
self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, article, desc);
|
||||
let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
|
||||
self.check_missing_docs_attrs(cx, attrs, impl_item.span, article, desc);
|
||||
}
|
||||
|
||||
fn check_struct_field(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::StructField<'_>) {
|
||||
if !sf.is_positional() {
|
||||
self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a", "struct field");
|
||||
let attrs = cx.tcx.hir().attrs(sf.hir_id);
|
||||
self.check_missing_docs_attrs(cx, attrs, sf.span, "a", "struct field");
|
||||
}
|
||||
}
|
||||
|
||||
fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
|
||||
self.check_missing_docs_attrs(cx, &v.attrs, v.span, "a", "variant");
|
||||
let attrs = cx.tcx.hir().attrs(v.id);
|
||||
self.check_missing_docs_attrs(cx, attrs, v.span, "a", "variant");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -93,7 +93,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
|
|||
match it.kind {
|
||||
hir::ItemKind::Fn(..) => {
|
||||
let desc = "a function";
|
||||
check_missing_inline_attrs(cx, &it.attrs, it.span, desc);
|
||||
let attrs = cx.tcx.hir().attrs(it.hir_id());
|
||||
check_missing_inline_attrs(cx, attrs, it.span, desc);
|
||||
},
|
||||
hir::ItemKind::Trait(ref _is_auto, ref _unsafe, ref _generics, ref _bounds, trait_items) => {
|
||||
// note: we need to check if the trait is exported so we can't use
|
||||
|
@ -108,7 +109,8 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
|
|||
// an impl is not provided
|
||||
let desc = "a default trait method";
|
||||
let item = cx.tcx.hir().trait_item(tit.id);
|
||||
check_missing_inline_attrs(cx, &item.attrs, item.span, desc);
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
check_missing_inline_attrs(cx, attrs, item.span, desc);
|
||||
}
|
||||
},
|
||||
}
|
||||
|
@ -160,6 +162,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingInline {
|
|||
}
|
||||
}
|
||||
|
||||
check_missing_inline_attrs(cx, &impl_item.attrs, impl_item.span, desc);
|
||||
let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
|
||||
check_missing_inline_attrs(cx, attrs, impl_item.span, desc);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -115,8 +115,9 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
||||
if is_automatically_derived(item.attrs) {
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
if is_automatically_derived(attrs) {
|
||||
debug_assert!(self.derived_item.is_none());
|
||||
self.derived_item = Some(item.def_id);
|
||||
}
|
||||
|
|
|
@ -80,13 +80,14 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
|
|||
}
|
||||
|
||||
match kind {
|
||||
FnKind::ItemFn(.., header, _, attrs) => {
|
||||
FnKind::ItemFn(.., header, _) => {
|
||||
let attrs = cx.tcx.hir().attrs(hir_id);
|
||||
if header.abi != Abi::Rust || requires_exact_signature(attrs) {
|
||||
return;
|
||||
}
|
||||
},
|
||||
FnKind::Method(..) => (),
|
||||
FnKind::Closure(..) => return,
|
||||
FnKind::Closure => return,
|
||||
}
|
||||
|
||||
// Exclude non-inherent impls
|
||||
|
|
|
@ -43,9 +43,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
|
|||
span: Span,
|
||||
hir_id: hir::HirId,
|
||||
) {
|
||||
if !matches!(fn_kind, FnKind::Closure(_))
|
||||
&& is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::result_type)
|
||||
{
|
||||
if !matches!(fn_kind, FnKind::Closure) && is_type_diagnostic_item(cx, return_ty(cx, hir_id), sym::result_type) {
|
||||
lint_impl_body(cx, span, body);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,8 @@ impl<'tcx> LateLintPass<'tcx> for PartialEqNeImpl {
|
|||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
||||
if_chain! {
|
||||
if let ItemKind::Impl(Impl { of_trait: Some(ref trait_ref), items: impl_items, .. }) = item.kind;
|
||||
if !is_automatically_derived(&*item.attrs);
|
||||
let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||
if !is_automatically_derived(attrs);
|
||||
if let Some(eq_trait) = cx.tcx.lang_items().eq_trait();
|
||||
if trait_ref.path.res.def_id() == eq_trait;
|
||||
then {
|
||||
|
|
|
@ -224,10 +224,11 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
|
|||
}
|
||||
|
||||
match kind {
|
||||
FnKind::ItemFn(.., header, _, attrs) => {
|
||||
FnKind::ItemFn(.., header, _) => {
|
||||
if header.abi != Abi::Rust {
|
||||
return;
|
||||
}
|
||||
let attrs = cx.tcx.hir().attrs(hir_id);
|
||||
for a in attrs {
|
||||
if let Some(meta_items) = a.meta_item_list() {
|
||||
if a.has_name(sym::proc_macro_derive)
|
||||
|
@ -239,7 +240,7 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
|
|||
}
|
||||
},
|
||||
FnKind::Method(..) => (),
|
||||
FnKind::Closure(..) => return,
|
||||
FnKind::Closure => return,
|
||||
}
|
||||
|
||||
// Exclude non-inherent impls
|
||||
|
|
|
@ -584,7 +584,7 @@ fn rvalue_locals(rvalue: &mir::Rvalue<'_>, mut visit: impl FnMut(mir::Local)) {
|
|||
match rvalue {
|
||||
Use(op) | Repeat(op, _) | Cast(_, op, _) | UnaryOp(_, op) => visit_op(op),
|
||||
Aggregate(_, ops) => ops.iter().for_each(visit_op),
|
||||
BinaryOp(_, lhs, rhs) | CheckedBinaryOp(_, lhs, rhs) => {
|
||||
BinaryOp(_, box (lhs, rhs)) | CheckedBinaryOp(_, box (lhs, rhs)) => {
|
||||
visit_op(lhs);
|
||||
visit_op(rhs);
|
||||
},
|
||||
|
|
|
@ -81,7 +81,7 @@ impl<'tcx> LateLintPass<'tcx> for Return {
|
|||
if let Some(stmt) = block.stmts.iter().last();
|
||||
if let StmtKind::Local(local) = &stmt.kind;
|
||||
if local.ty.is_none();
|
||||
if local.attrs.is_empty();
|
||||
if cx.tcx.hir().attrs(local.hir_id).is_empty();
|
||||
if let Some(initexpr) = &local.init;
|
||||
if let PatKind::Binding(.., ident, _) = local.pat.kind;
|
||||
if let ExprKind::Path(qpath) = &retexpr.kind;
|
||||
|
@ -131,7 +131,7 @@ impl<'tcx> LateLintPass<'tcx> for Return {
|
|||
_: HirId,
|
||||
) {
|
||||
match kind {
|
||||
FnKind::Closure(_) => {
|
||||
FnKind::Closure => {
|
||||
// when returning without value in closure, replace this `return`
|
||||
// with an empty block to prevent invalid suggestion (see #6501)
|
||||
let replacement = if let ExprKind::Ret(None) = &body.value.kind {
|
||||
|
@ -177,7 +177,8 @@ fn check_final_expr<'tcx>(
|
|||
// simple return is always "bad"
|
||||
ExprKind::Ret(ref inner) => {
|
||||
// allow `#[cfg(a)] return a; #[cfg(b)] return b;`
|
||||
if !expr.attrs.iter().any(attr_is_cfg) {
|
||||
let attrs = cx.tcx.hir().attrs(expr.hir_id);
|
||||
if !attrs.iter().any(attr_is_cfg) {
|
||||
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
|
||||
if !borrows {
|
||||
emit_return_lint(
|
||||
|
|
|
@ -199,7 +199,7 @@ fn check_for_slice<'a>(cx: &LateContext<'_>, lhs1: &'a Expr<'_>, lhs2: &'a Expr<
|
|||
if matches!(ty.kind(), ty::Slice(_))
|
||||
|| matches!(ty.kind(), ty::Array(_, _))
|
||||
|| is_type_diagnostic_item(cx, ty, sym::vec_type)
|
||||
|| is_type_diagnostic_item(cx, ty, sym!(vecdeque_type))
|
||||
|| is_type_diagnostic_item(cx, ty, sym::vecdeque_type)
|
||||
{
|
||||
return Slice::Swappable(lhs1, idx1, idx2);
|
||||
}
|
||||
|
|
|
@ -1516,14 +1516,14 @@ impl<'tcx> ImplicitHasherType<'tcx> {
|
|||
|
||||
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
|
||||
|
||||
if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) && params_len == 2 {
|
||||
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) && params_len == 2 {
|
||||
Some(ImplicitHasherType::HashMap(
|
||||
hir_ty.span,
|
||||
ty,
|
||||
snippet(cx, params[0].span, "K"),
|
||||
snippet(cx, params[1].span, "V"),
|
||||
))
|
||||
} else if is_type_diagnostic_item(cx, ty, sym!(hashset_type)) && params_len == 1 {
|
||||
} else if is_type_diagnostic_item(cx, ty, sym::hashset_type) && params_len == 1 {
|
||||
Some(ImplicitHasherType::HashSet(
|
||||
hir_ty.span,
|
||||
ty,
|
||||
|
|
|
@ -66,12 +66,12 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
|
|||
) {
|
||||
// Abort if public function/method or closure.
|
||||
match fn_kind {
|
||||
FnKind::ItemFn(.., visibility, _) | FnKind::Method(.., Some(visibility), _) => {
|
||||
FnKind::ItemFn(.., visibility) | FnKind::Method(.., Some(visibility)) => {
|
||||
if visibility.node.is_pub() {
|
||||
return;
|
||||
}
|
||||
},
|
||||
FnKind::Closure(..) => return,
|
||||
FnKind::Closure => return,
|
||||
_ => (),
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
//! to generate a clippy lint detecting said code automatically.
|
||||
|
||||
use crate::utils::get_attr;
|
||||
use rustc_ast::ast::{Attribute, LitFloatType, LitKind};
|
||||
use rustc_ast::ast::{LitFloatType, LitKind};
|
||||
use rustc_ast::walk_list;
|
||||
use rustc_data_structures::fx::FxHashMap;
|
||||
use rustc_hir as hir;
|
||||
|
@ -10,7 +10,6 @@ use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
|
|||
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, Pat, PatKind, QPath, Stmt, StmtKind, TyKind};
|
||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||
use rustc_middle::hir::map::Map;
|
||||
use rustc_session::Session;
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
|
||||
declare_clippy_lint! {
|
||||
|
@ -66,7 +65,7 @@ fn done() {
|
|||
|
||||
impl<'tcx> LateLintPass<'tcx> for Author {
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
|
||||
if !has_attr(cx.sess(), &item.attrs) {
|
||||
if !has_attr(cx, item.hir_id()) {
|
||||
return;
|
||||
}
|
||||
prelude();
|
||||
|
@ -75,7 +74,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
|
|||
}
|
||||
|
||||
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
|
||||
if !has_attr(cx.sess(), &item.attrs) {
|
||||
if !has_attr(cx, item.hir_id()) {
|
||||
return;
|
||||
}
|
||||
prelude();
|
||||
|
@ -84,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
|
|||
}
|
||||
|
||||
fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
|
||||
if !has_attr(cx.sess(), &item.attrs) {
|
||||
if !has_attr(cx, item.hir_id()) {
|
||||
return;
|
||||
}
|
||||
prelude();
|
||||
|
@ -93,7 +92,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
|
|||
}
|
||||
|
||||
fn check_variant(&mut self, cx: &LateContext<'tcx>, var: &'tcx hir::Variant<'_>) {
|
||||
if !has_attr(cx.sess(), &var.attrs) {
|
||||
if !has_attr(cx, var.id) {
|
||||
return;
|
||||
}
|
||||
prelude();
|
||||
|
@ -103,7 +102,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
|
|||
}
|
||||
|
||||
fn check_struct_field(&mut self, cx: &LateContext<'tcx>, field: &'tcx hir::StructField<'_>) {
|
||||
if !has_attr(cx.sess(), &field.attrs) {
|
||||
if !has_attr(cx, field.hir_id) {
|
||||
return;
|
||||
}
|
||||
prelude();
|
||||
|
@ -112,7 +111,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
|
|||
}
|
||||
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
||||
if !has_attr(cx.sess(), &expr.attrs) {
|
||||
if !has_attr(cx, expr.hir_id) {
|
||||
return;
|
||||
}
|
||||
prelude();
|
||||
|
@ -121,7 +120,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
|
|||
}
|
||||
|
||||
fn check_arm(&mut self, cx: &LateContext<'tcx>, arm: &'tcx hir::Arm<'_>) {
|
||||
if !has_attr(cx.sess(), &arm.attrs) {
|
||||
if !has_attr(cx, arm.hir_id) {
|
||||
return;
|
||||
}
|
||||
prelude();
|
||||
|
@ -130,7 +129,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
|
|||
}
|
||||
|
||||
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx hir::Stmt<'_>) {
|
||||
if !has_attr(cx.sess(), stmt.kind.attrs(|id| cx.tcx.hir().item(id))) {
|
||||
if !has_attr(cx, stmt.hir_id) {
|
||||
return;
|
||||
}
|
||||
prelude();
|
||||
|
@ -139,7 +138,7 @@ impl<'tcx> LateLintPass<'tcx> for Author {
|
|||
}
|
||||
|
||||
fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ForeignItem<'_>) {
|
||||
if !has_attr(cx.sess(), &item.attrs) {
|
||||
if !has_attr(cx, item.hir_id()) {
|
||||
return;
|
||||
}
|
||||
prelude();
|
||||
|
@ -719,8 +718,9 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
|
|||
}
|
||||
}
|
||||
|
||||
fn has_attr(sess: &Session, attrs: &[Attribute]) -> bool {
|
||||
get_attr(sess, attrs, "author").count() > 0
|
||||
fn has_attr(cx: &LateContext<'_>, hir_id: hir::HirId) -> bool {
|
||||
let attrs = cx.tcx.hir().attrs(hir_id);
|
||||
get_attr(cx.sess(), attrs, "author").count() > 0
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
|
|
|
@ -33,14 +33,14 @@ declare_lint_pass!(DeepCodeInspector => [DEEP_CODE_INSPECTION]);
|
|||
|
||||
impl<'tcx> LateLintPass<'tcx> for DeepCodeInspector {
|
||||
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
|
||||
if !has_attr(cx.sess(), &item.attrs) {
|
||||
if !has_attr(cx.sess(), cx.tcx.hir().attrs(item.hir_id())) {
|
||||
return;
|
||||
}
|
||||
print_item(cx, item);
|
||||
}
|
||||
|
||||
fn check_impl_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::ImplItem<'_>) {
|
||||
if !has_attr(cx.sess(), &item.attrs) {
|
||||
if !has_attr(cx.sess(), cx.tcx.hir().attrs(item.hir_id())) {
|
||||
return;
|
||||
}
|
||||
println!("impl item `{}`", item.ident.name);
|
||||
|
@ -89,14 +89,14 @@ impl<'tcx> LateLintPass<'tcx> for DeepCodeInspector {
|
|||
//
|
||||
|
||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
|
||||
if !has_attr(cx.sess(), &expr.attrs) {
|
||||
if !has_attr(cx.sess(), cx.tcx.hir().attrs(expr.hir_id)) {
|
||||
return;
|
||||
}
|
||||
print_expr(cx, expr, 0);
|
||||
}
|
||||
|
||||
fn check_arm(&mut self, cx: &LateContext<'tcx>, arm: &'tcx hir::Arm<'_>) {
|
||||
if !has_attr(cx.sess(), &arm.attrs) {
|
||||
if !has_attr(cx.sess(), cx.tcx.hir().attrs(arm.hir_id)) {
|
||||
return;
|
||||
}
|
||||
print_pat(cx, &arm.pat, 1);
|
||||
|
@ -109,7 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for DeepCodeInspector {
|
|||
}
|
||||
|
||||
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx hir::Stmt<'_>) {
|
||||
if !has_attr(cx.sess(), stmt.kind.attrs(|id| cx.tcx.hir().item(id))) {
|
||||
if !has_attr(cx.sess(), cx.tcx.hir().attrs(stmt.hir_id)) {
|
||||
return;
|
||||
}
|
||||
match stmt.kind {
|
||||
|
|
|
@ -3,6 +3,7 @@ use rustc_hir::{self as hir, HirId, ItemKind, Node};
|
|||
use rustc_lint::{LateContext, LateLintPass};
|
||||
use rustc_middle::ty::{Adt, Ty};
|
||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||
use rustc_span::sym;
|
||||
use rustc_target::abi::LayoutOf as _;
|
||||
use rustc_typeck::hir_ty_to_ty;
|
||||
|
||||
|
@ -47,7 +48,7 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
|
|||
if !hir_ty.span.from_expansion();
|
||||
if !in_trait_impl(cx, hir_ty.hir_id);
|
||||
let ty = ty_from_hir_ty(cx, hir_ty);
|
||||
if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) || match_type(cx, ty, &paths::BTREEMAP);
|
||||
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) || match_type(cx, ty, &paths::BTREEMAP);
|
||||
if let Adt(_, ref substs) = ty.kind();
|
||||
let ty = substs.type_at(1);
|
||||
// Do this to prevent `layout_of` crashing, being unable to fully normalize `ty`.
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
//! - or-fun-call
|
||||
//! - option-if-let-else
|
||||
|
||||
use crate::{is_ctor_or_promotable_const_function, is_type_diagnostic_item, match_type, paths};
|
||||
use crate::{is_ctor_or_promotable_const_function, is_type_diagnostic_item};
|
||||
use rustc_hir::def::{DefKind, Res};
|
||||
|
||||
use rustc_hir::intravisit;
|
||||
|
@ -18,6 +18,7 @@ use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
|
|||
use rustc_hir::{Block, Expr, ExprKind, Path, QPath};
|
||||
use rustc_lint::LateContext;
|
||||
use rustc_middle::hir::map::Map;
|
||||
use rustc_span::sym;
|
||||
|
||||
/// Is the expr pure (is it free from side-effects)?
|
||||
/// This function is named so to stress that it isn't exhaustive and returns FNs.
|
||||
|
@ -99,8 +100,8 @@ fn identify_some_potentially_expensive_patterns<'tcx>(cx: &LateContext<'tcx>, ex
|
|||
ExprKind::Call(..) => !is_ctor_or_promotable_const_function(self.cx, expr),
|
||||
ExprKind::Index(obj, _) => {
|
||||
let ty = self.cx.typeck_results().expr_ty(obj);
|
||||
is_type_diagnostic_item(self.cx, ty, sym!(hashmap_type))
|
||||
|| match_type(self.cx, ty, &paths::BTREEMAP)
|
||||
is_type_diagnostic_item(self.cx, ty, sym::hashmap_type)
|
||||
|| is_type_diagnostic_item(self.cx, ty, sym::BTreeMap)
|
||||
},
|
||||
ExprKind::MethodCall(..) => true,
|
||||
_ => false,
|
||||
|
|
|
@ -63,9 +63,9 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE};
|
|||
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
|
||||
use rustc_hir::Node;
|
||||
use rustc_hir::{
|
||||
def, Arm, Block, Body, Constness, Crate, Expr, ExprKind, FnDecl, GenericArgs, HirId, Impl, ImplItem, ImplItemKind,
|
||||
Item, ItemKind, LangItem, MatchSource, Param, Pat, PatKind, Path, PathSegment, QPath, TraitItem, TraitItemKind,
|
||||
TraitRef, TyKind, Unsafety,
|
||||
def, Arm, Block, Body, Constness, Expr, ExprKind, FnDecl, GenericArgs, HirId, Impl, ImplItem, ImplItemKind, Item,
|
||||
ItemKind, LangItem, MatchSource, Param, Pat, PatKind, Path, PathSegment, QPath, TraitItem, TraitItemKind, TraitRef,
|
||||
TyKind, Unsafety,
|
||||
};
|
||||
use rustc_infer::infer::TyCtxtInferExt;
|
||||
use rustc_lint::{LateContext, Level, Lint, LintContext};
|
||||
|
@ -1431,24 +1431,24 @@ pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_>, node: HirId) -> bool
|
|||
}
|
||||
|
||||
/// Returns true if ty has `iter` or `iter_mut` methods
|
||||
pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<&'static str> {
|
||||
pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<Symbol> {
|
||||
// FIXME: instead of this hard-coded list, we should check if `<adt>::iter`
|
||||
// exists and has the desired signature. Unfortunately FnCtxt is not exported
|
||||
// so we can't use its `lookup_method` method.
|
||||
let into_iter_collections: [&[&str]; 13] = [
|
||||
&paths::VEC,
|
||||
&paths::OPTION,
|
||||
&paths::RESULT,
|
||||
&paths::BTREESET,
|
||||
&paths::BTREEMAP,
|
||||
&paths::VEC_DEQUE,
|
||||
&paths::LINKED_LIST,
|
||||
&paths::BINARY_HEAP,
|
||||
&paths::HASHSET,
|
||||
&paths::HASHMAP,
|
||||
&paths::PATH_BUF,
|
||||
&paths::PATH,
|
||||
&paths::RECEIVER,
|
||||
let into_iter_collections: &[Symbol] = &[
|
||||
sym::vec_type,
|
||||
sym::option_type,
|
||||
sym::result_type,
|
||||
sym::BTreeMap,
|
||||
sym::BTreeSet,
|
||||
sym::vecdeque_type,
|
||||
sym::LinkedList,
|
||||
sym::BinaryHeap,
|
||||
sym::hashset_type,
|
||||
sym::hashmap_type,
|
||||
sym::PathBuf,
|
||||
sym::Path,
|
||||
sym::Receiver,
|
||||
];
|
||||
|
||||
let ty_to_check = match probably_ref_ty.kind() {
|
||||
|
@ -1457,15 +1457,15 @@ pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<
|
|||
};
|
||||
|
||||
let def_id = match ty_to_check.kind() {
|
||||
ty::Array(..) => return Some("array"),
|
||||
ty::Slice(..) => return Some("slice"),
|
||||
ty::Array(..) => return Some(sym::array),
|
||||
ty::Slice(..) => return Some(sym::slice),
|
||||
ty::Adt(adt, _) => adt.did,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
for path in &into_iter_collections {
|
||||
if match_def_path(cx, def_id, path) {
|
||||
return Some(*path.last().unwrap());
|
||||
for &name in into_iter_collections {
|
||||
if cx.tcx.is_diagnostic_item(name, def_id) {
|
||||
return Some(cx.tcx.item_name(def_id));
|
||||
}
|
||||
}
|
||||
None
|
||||
|
@ -1682,8 +1682,8 @@ pub fn is_must_use_func_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
|
|||
did.map_or(false, |did| must_use_attr(&cx.tcx.get_attrs(did)).is_some())
|
||||
}
|
||||
|
||||
pub fn is_no_std_crate(krate: &Crate<'_>) -> bool {
|
||||
krate.item.attrs.iter().any(|attr| {
|
||||
pub fn is_no_std_crate(cx: &LateContext<'_>) -> bool {
|
||||
cx.tcx.hir().attrs(hir::CRATE_HIR_ID).iter().any(|attr| {
|
||||
if let ast::AttrKind::Normal(ref attr, _) = attr.kind {
|
||||
attr.path == sym::no_std
|
||||
} else {
|
||||
|
|
|
@ -93,7 +93,6 @@ pub(super) const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"];
|
|||
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
|
||||
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
|
||||
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];
|
||||
pub const PATH: [&str; 3] = ["std", "path", "Path"];
|
||||
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
|
||||
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
|
||||
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];
|
||||
|
@ -109,7 +108,6 @@ pub const PTR_SWAP_NONOVERLAPPING: [&str; 3] = ["core", "ptr", "swap_nonoverlapp
|
|||
pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"];
|
||||
pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"];
|
||||
pub const RC_PTR_EQ: [&str; 4] = ["alloc", "rc", "Rc", "ptr_eq"];
|
||||
pub const RECEIVER: [&str; 4] = ["std", "sync", "mpsc", "Receiver"];
|
||||
pub const REFCELL_REF: [&str; 3] = ["core", "cell", "Ref"];
|
||||
pub const REFCELL_REFMUT: [&str; 3] = ["core", "cell", "RefMut"];
|
||||
pub const REGEX_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "unicode", "RegexBuilder", "new"];
|
||||
|
|
|
@ -172,7 +172,7 @@ fn check_rvalue(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId, rvalue: &Rv
|
|||
}
|
||||
},
|
||||
// binops are fine on integers
|
||||
Rvalue::BinaryOp(_, lhs, rhs) | Rvalue::CheckedBinaryOp(_, lhs, rhs) => {
|
||||
Rvalue::BinaryOp(_, box (lhs, rhs)) | Rvalue::CheckedBinaryOp(_, box (lhs, rhs)) => {
|
||||
check_operand(tcx, lhs, span, body)?;
|
||||
check_operand(tcx, rhs, span, body)?;
|
||||
let ty = lhs.ty(body, tcx);
|
||||
|
@ -210,7 +210,7 @@ fn check_statement(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId, statemen
|
|||
StatementKind::Assign(box (place, rval)) => {
|
||||
check_place(tcx, *place, span, body)?;
|
||||
check_rvalue(tcx, body, def_id, rval, span)
|
||||
},
|
||||
}
|
||||
|
||||
StatementKind::FakeRead(_, place) |
|
||||
// just an assignment
|
||||
|
@ -218,6 +218,13 @@ fn check_statement(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId, statemen
|
|||
|
||||
StatementKind::LlvmInlineAsm { .. } => Err((span, "cannot use inline assembly in const fn".into())),
|
||||
|
||||
StatementKind::CopyNonOverlapping(box rustc_middle::mir::CopyNonOverlapping{
|
||||
dst, src, count,
|
||||
}) => {
|
||||
check_operand(tcx, dst, span, body)?;
|
||||
check_operand(tcx, src, span, body)?;
|
||||
check_operand(tcx, count, span, body)
|
||||
}
|
||||
// These are all NOPs
|
||||
StatementKind::StorageLive(_)
|
||||
| StatementKind::StorageDead(_)
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
[toolchain]
|
||||
channel = "nightly-2021-02-25"
|
||||
channel = "nightly-2021-03-11"
|
||||
components = ["llvm-tools-preview", "rustc-dev", "rust-src"]
|
||||
|
|
|
@ -120,7 +120,7 @@ impl rustc_driver::Callbacks for ClippyCallbacks {
|
|||
// run on the unoptimized MIR. On the other hand this results in some false negatives. If
|
||||
// MIR passes can be enabled / disabled separately, we should figure out, what passes to
|
||||
// use for Clippy.
|
||||
config.opts.debugging_opts.mir_opt_level = 0;
|
||||
config.opts.debugging_opts.mir_opt_level = Some(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,12 @@ use rustc_tools_util::VersionInfo;
|
|||
|
||||
#[test]
|
||||
fn check_that_clippy_lints_and_clippy_utils_have_the_same_version_as_clippy() {
|
||||
// do not run this test inside the upstream rustc repo:
|
||||
// https://github.com/rust-lang/rust-clippy/issues/6683
|
||||
if option_env!("RUSTC_TEST_SUITE").is_some() {
|
||||
return;
|
||||
}
|
||||
|
||||
let clippy_meta = cargo_metadata::MetadataCommand::new()
|
||||
.no_deps()
|
||||
.exec()
|
||||
|
|
Loading…
Reference in a new issue