Skip to content

Commit

Permalink
clippy: handle supertraits of Sized
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Markeffsky committed Mar 19, 2024
1 parent c02b3c7 commit e399970
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use rustc_middle::ty::{
};
use rustc_span::{sym, Symbol};
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
use rustc_trait_selection::traits::{Obligation, ObligationCause};
use rustc_trait_selection::traits::{self, Obligation, ObligationCause};

use super::UNNECESSARY_TO_OWNED;

Expand Down Expand Up @@ -323,9 +323,10 @@ fn check_other_call_arg<'tcx>(
&& let (input, n_refs) = peel_mid_ty_refs(*input)
&& let (trait_predicates, _) = get_input_traits_and_projections(cx, callee_def_id, input)
&& let Some(sized_def_id) = cx.tcx.lang_items().sized_trait()
&& let sized_super_def_ids = traits::supertrait_def_ids(cx.tcx, sized_def_id).collect::<Vec<_>>()
&& let [trait_predicate] = trait_predicates
.iter()
.filter(|trait_predicate| trait_predicate.def_id() != sized_def_id)
.filter(|trait_predicate| !sized_super_def_ids.contains(&trait_predicate.def_id()))
.collect::<Vec<_>>()[..]
&& let Some(deref_trait_id) = cx.tcx.get_diagnostic_item(sym::Deref)
&& let Some(as_ref_trait_id) = cx.tcx.get_diagnostic_item(sym::AsRef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc_middle::ty::{
use rustc_session::impl_lint_pass;
use rustc_span::symbol::sym;
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
use rustc_trait_selection::traits::{Obligation, ObligationCause};
use rustc_trait_selection::traits::{self, Obligation, ObligationCause};
use std::collections::VecDeque;

declare_clippy_lint! {
Expand Down Expand Up @@ -171,6 +171,10 @@ fn needless_borrow_count<'tcx>(
let sized_trait_def_id = cx.tcx.lang_items().sized_trait();
let drop_trait_def_id = cx.tcx.lang_items().drop_trait();

let sized_super_def_ids = sized_trait_def_id.map_or_else(Vec::new, |sized_def_id| {
traits::supertrait_def_ids(cx.tcx, sized_def_id).collect()
});

let fn_sig = cx.tcx.fn_sig(fn_id).instantiate_identity().skip_binder();
let predicates = cx.tcx.param_env(fn_id).caller_bounds();
let projection_predicates = predicates
Expand Down Expand Up @@ -203,7 +207,7 @@ fn needless_borrow_count<'tcx>(
})
.all(|trait_def_id| {
Some(trait_def_id) == destruct_trait_def_id
|| Some(trait_def_id) == sized_trait_def_id
|| sized_super_def_ids.contains(&trait_def_id)
|| cx.tcx.is_diagnostic_item(sym::Any, trait_def_id)
})
{
Expand Down
3 changes: 2 additions & 1 deletion src/tools/clippy/clippy_lints/src/needless_pass_by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
];

let sized_trait = need!(cx.tcx.lang_items().sized_trait());
let sized_super_traits = traits::supertrait_def_ids(cx.tcx, sized_trait).collect::<Vec<_>>();

let preds = traits::elaborate(cx.tcx, cx.param_env.caller_bounds().iter())
.filter(|p| !p.is_global())
.filter_map(|pred| {
// Note that we do not want to deal with qualified predicates here.
match pred.kind().no_bound_vars() {
Some(ty::ClauseKind::Trait(pred)) if pred.def_id() != sized_trait => Some(pred),
Some(ty::ClauseKind::Trait(pred)) if !sized_super_traits.contains(&pred.def_id()) => Some(pred),
_ => None,
}
})
Expand Down

0 comments on commit e399970

Please sign in to comment.