get clippy to compile again

This commit is contained in:
lcnr 2022-03-18 13:33:40 +01:00
parent b3535a1549
commit 148b593954
7 changed files with 26 additions and 59 deletions

View File

@ -3,7 +3,7 @@ use clippy_utils::{higher, peel_blocks_with_stmt, SpanlessEq};
use if_chain::if_chain; use if_chain::if_chain;
use rustc_ast::ast::LitKind; use rustc_ast::ast::LitKind;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::{lang_items::LangItem, BinOpKind, Expr, ExprKind, QPath}; use rustc_hir::{BinOpKind, Expr, ExprKind, QPath};
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};
@ -82,14 +82,6 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
// Get the variable name // Get the variable name
let var_name = ares_path.segments[0].ident.name.as_str(); let var_name = ares_path.segments[0].ident.name.as_str();
const INT_TYPES: [LangItem; 5] = [
LangItem::I8,
LangItem::I16,
LangItem::I32,
LangItem::I64,
LangItem::Isize
];
match cond_num_val.kind { match cond_num_val.kind {
ExprKind::Lit(ref cond_lit) => { ExprKind::Lit(ref cond_lit) => {
// Check if the constant is zero // Check if the constant is zero
@ -105,8 +97,8 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
if name.ident.as_str() == "MIN"; if name.ident.as_str() == "MIN";
if let Some(const_id) = cx.typeck_results().type_dependent_def_id(cond_num_val.hir_id); if let Some(const_id) = cx.typeck_results().type_dependent_def_id(cond_num_val.hir_id);
if let Some(impl_id) = cx.tcx.impl_of_method(const_id); if let Some(impl_id) = cx.tcx.impl_of_method(const_id);
let mut int_ids = INT_TYPES.iter().filter_map(|&ty| cx.tcx.lang_items().require(ty).ok()); if let None = cx.tcx.impl_trait_ref(impl_id); // An inherent impl
if int_ids.any(|int_id| int_id == impl_id); if cx.tcx.type_of(impl_id).is_integral();
then { then {
print_lint_and_sugg(cx, var_name, expr) print_lint_and_sugg(cx, var_name, expr)
} }
@ -118,8 +110,8 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
if name.ident.as_str() == "min_value"; if name.ident.as_str() == "min_value";
if let Some(func_id) = cx.typeck_results().type_dependent_def_id(func.hir_id); if let Some(func_id) = cx.typeck_results().type_dependent_def_id(func.hir_id);
if let Some(impl_id) = cx.tcx.impl_of_method(func_id); if let Some(impl_id) = cx.tcx.impl_of_method(func_id);
let mut int_ids = INT_TYPES.iter().filter_map(|&ty| cx.tcx.lang_items().require(ty).ok()); if let None = cx.tcx.impl_trait_ref(impl_id); // An inherent impl
if int_ids.any(|int_id| int_id == impl_id); if cx.tcx.type_of(impl_id).is_integral();
then { then {
print_lint_and_sugg(cx, var_name, expr) print_lint_and_sugg(cx, var_name, expr)
} }

View File

@ -49,10 +49,11 @@ pub fn is_clone_like(cx: &LateContext<'_>, method_name: &str, method_def_id: hir
"to_owned" => is_diag_trait_item(cx, method_def_id, sym::ToOwned), "to_owned" => is_diag_trait_item(cx, method_def_id, sym::ToOwned),
"to_path_buf" => is_diag_item_method(cx, method_def_id, sym::Path), "to_path_buf" => is_diag_item_method(cx, method_def_id, sym::Path),
"to_vec" => { "to_vec" => {
cx.tcx cx.tcx.impl_of_method(method_def_id)
.impl_of_method(method_def_id) .filter(|&impl_did| {
.map(|impl_did| Some(impl_did) == cx.tcx.lang_items().slice_alloc_impl()) cx.tcx.type_of(impl_did).is_slice() && cx.tcx.impl_trait_ref(impl_did).is_none()
== Some(true) })
.is_some()
}, },
_ => false, _ => false,
} }

View File

@ -12,13 +12,13 @@ pub(super) fn check(cx: &LateContext<'_>, method_name: &str, expr: &Expr<'_>, se
if count <= 1; if count <= 1;
if let Some(call_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); if let Some(call_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
if let Some(impl_id) = cx.tcx.impl_of_method(call_id); if let Some(impl_id) = cx.tcx.impl_of_method(call_id);
let lang_items = cx.tcx.lang_items(); if cx.tcx.impl_trait_ref(impl_id).is_none();
if lang_items.slice_impl() == Some(impl_id) || lang_items.str_impl() == Some(impl_id); let self_ty = cx.tcx.type_of(impl_id);
if self_ty.is_slice() || self_ty.is_str();
then { then {
// Ignore empty slice and string literals when used with a literal count. // Ignore empty slice and string literals when used with a literal count.
if matches!(self_arg.kind, ExprKind::Array([])) if matches!(self_arg.kind, ExprKind::Array([]))
|| matches!(self_arg.kind, ExprKind::Lit(Spanned { node: LitKind::Str(s, _), .. }) if s.is_empty()) || matches!(self_arg.kind, ExprKind::Lit(Spanned { node: LitKind::Str(s, _), .. }) if s.is_empty())
{ {
return; return;
} }
@ -28,7 +28,7 @@ pub(super) fn check(cx: &LateContext<'_>, method_name: &str, expr: &Expr<'_>, se
"the resulting iterator will always return `None`") "the resulting iterator will always return `None`")
} else { } else {
(format!("`{}` called with `1` split", method_name), (format!("`{}` called with `1` split", method_name),
if lang_items.slice_impl() == Some(impl_id) { if self_ty.is_slice() {
"the resulting iterator will always return the entire slice followed by `None`" "the resulting iterator will always return the entire slice followed by `None`"
} else { } else {
"the resulting iterator will always return the entire string followed by `None`" "the resulting iterator will always return the entire string followed by `None`"

View File

@ -16,7 +16,7 @@ use rustc_hir::{
}; };
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter; use rustc_middle::hir::nested_filter;
use rustc_middle::ty::{self, AssocItems, AssocKind, Ty}; use rustc_middle::ty::{self, Ty};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span; use rustc_span::source_map::Span;
use rustc_span::symbol::Symbol; use rustc_span::symbol::Symbol;
@ -308,7 +308,6 @@ struct PtrArg<'tcx> {
method_renames: &'static [(&'static str, &'static str)], method_renames: &'static [(&'static str, &'static str)],
ref_prefix: RefPrefix, ref_prefix: RefPrefix,
deref_ty: DerefTy<'tcx>, deref_ty: DerefTy<'tcx>,
deref_assoc_items: Option<(DefId, &'tcx AssocItems<'tcx>)>,
} }
impl PtrArg<'_> { impl PtrArg<'_> {
fn build_msg(&self) -> String { fn build_msg(&self) -> String {
@ -411,7 +410,7 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
if params.get(i).map_or(true, |p| !is_lint_allowed(cx, PTR_ARG, p.hir_id)); if params.get(i).map_or(true, |p| !is_lint_allowed(cx, PTR_ARG, p.hir_id));
then { then {
let (method_renames, deref_ty, deref_impl_id) = match cx.tcx.get_diagnostic_name(adt.did()) { let (method_renames, deref_ty) = match cx.tcx.get_diagnostic_name(adt.did()) {
Some(sym::Vec) => ( Some(sym::Vec) => (
[("clone", ".to_owned()")].as_slice(), [("clone", ".to_owned()")].as_slice(),
DerefTy::Slice( DerefTy::Slice(
@ -424,17 +423,14 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
}), }),
substs.type_at(0), substs.type_at(0),
), ),
cx.tcx.lang_items().slice_impl()
), ),
Some(sym::String) => ( Some(sym::String) => (
[("clone", ".to_owned()"), ("as_str", "")].as_slice(), [("clone", ".to_owned()"), ("as_str", "")].as_slice(),
DerefTy::Str, DerefTy::Str,
cx.tcx.lang_items().str_impl()
), ),
Some(sym::PathBuf) => ( Some(sym::PathBuf) => (
[("clone", ".to_path_buf()"), ("as_path", "")].as_slice(), [("clone", ".to_path_buf()"), ("as_path", "")].as_slice(),
DerefTy::Path, DerefTy::Path,
None,
), ),
Some(sym::Cow) if mutability == Mutability::Not => { Some(sym::Cow) if mutability == Mutability::Not => {
let ty_name = name.args let ty_name = name.args
@ -470,7 +466,6 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
mutability, mutability,
}, },
deref_ty, deref_ty,
deref_assoc_items: deref_impl_id.map(|id| (id, cx.tcx.associated_items(id))),
}); });
} }
} }
@ -607,14 +602,7 @@ fn check_ptr_arg_usage<'tcx>(cx: &LateContext<'tcx>, body: &'tcx Body<'_>, args:
// If the types match check for methods which exist on both types. e.g. `Vec::len` and // If the types match check for methods which exist on both types. e.g. `Vec::len` and
// `slice::len` // `slice::len`
ty::Adt(def, _) ty::Adt(def, _)
if def.did() == args.ty_did if def.did() == args.ty_did =>
&& (i != 0
|| self.cx.tcx.trait_of_item(id).is_some()
|| !args.deref_assoc_items.map_or(false, |(id, items)| {
items
.find_by_name_and_kind(self.cx.tcx, name.ident, AssocKind::Fn, id)
.is_some()
})) =>
{ {
set_skip_flag(); set_skip_flag();
}, },

View File

@ -77,9 +77,9 @@ use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
use rustc_hir::itemlikevisit::ItemLikeVisitor; use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::LangItem::{OptionNone, ResultErr, ResultOk}; use rustc_hir::LangItem::{OptionNone, ResultErr, ResultOk};
use rustc_hir::{ use rustc_hir::{
def, lang_items, Arm, ArrayLen, BindingAnnotation, Block, BlockCheckMode, Body, Constness, Destination, Expr, def, Arm, ArrayLen, BindingAnnotation, Block, BlockCheckMode, Body, Constness, Destination, Expr,
ExprKind, FnDecl, ForeignItem, HirId, Impl, ImplItem, ImplItemKind, IsAsync, Item, ItemKind, LangItem, Local, ExprKind, FnDecl, ForeignItem, HirId, Impl, ImplItem, ImplItemKind, IsAsync, Item, ItemKind, LangItem, Local,
MatchSource, Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, Target, MatchSource, Mutability, Node, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind,
TraitItem, TraitItemKind, TraitRef, TyKind, UnOp, TraitItem, TraitItemKind, TraitRef, TyKind, UnOp,
}; };
use rustc_lint::{LateContext, Level, Lint, LintContext}; use rustc_lint::{LateContext, Level, Lint, LintContext};
@ -479,12 +479,10 @@ pub fn def_path_res(cx: &LateContext<'_>, path: &[&str]) -> Res {
_ => None, _ => None,
} }
} }
fn find_primitive(tcx: TyCtxt<'_>, name: &str) -> Option<DefId> { fn find_primitive(_tcx: TyCtxt<'_>, _name: &str) -> Option<DefId> {
if let Some(&(index, Target::Impl)) = lang_items::ITEM_REFS.get(&Symbol::intern(name)) { // FIXME: Deal with this without relying on lang items or by only
tcx.lang_items().items()[index] // looking at a single impl.
} else { None
None
}
} }
fn find_crate(tcx: TyCtxt<'_>, name: &str) -> Option<DefId> { fn find_crate(tcx: TyCtxt<'_>, name: &str) -> Option<DefId> {
tcx.crates(()) tcx.crates(())

View File

@ -10,8 +10,8 @@ fn main() {
let mut a = vec![1, 2, 3, 4]; let mut a = vec![1, 2, 3, 4];
a.iter().sum::<i32>(); a.iter().sum::<i32>();
a.sort_unstable(); a.sort_unstable(); // FIXME: Warn here
let _ = 2.0f32.clamp(3.0f32, 4.0f32); let _ = 2.0f32.clamp(3.0f32, 4.0f32); // FIXME: Warn here
let _ = 2.0f64.clamp(3.0f64, 4.0f64); let _ = 2.0f64.clamp(3.0f64, 4.0f64);
} }

View File

@ -20,17 +20,5 @@ error: use of a disallowed method `std::iter::Iterator::sum`
LL | a.iter().sum::<i32>(); LL | a.iter().sum::<i32>();
| ^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^
error: use of a disallowed method `slice::sort_unstable` error: aborting due to 3 previous errors
--> $DIR/conf_disallowed_methods.rs:13:5
|
LL | a.sort_unstable();
| ^^^^^^^^^^^^^^^^^
error: use of a disallowed method `f32::clamp`
--> $DIR/conf_disallowed_methods.rs:15:13
|
LL | let _ = 2.0f32.clamp(3.0f32, 4.0f32);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 5 previous errors