Skip to content

Commit faa2adc

Browse files
committed
WIP: modify get method function
1 parent f060d81 commit faa2adc

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

lints/par_iter/src/lib.rs

+38-25
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@ extern crate rustc_trait_selection;
1515
mod constants;
1616
mod variable_check;
1717

18-
use clippy_utils::{get_parent_expr, get_trait_def_id};
18+
use clippy_utils::get_parent_expr;
1919
use rustc_data_structures::fx::FxHashSet;
2020
use rustc_errors::Applicability;
2121
use rustc_hir::intravisit::{walk_expr, Visitor};
2222
use rustc_hir::{self as hir};
23+
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
24+
use rustc_infer::infer::TyCtxtInferExt;
2325
use rustc_lint::{LateContext, LateLintPass, LintContext};
24-
use rustc_middle::ty::{self};
25-
use rustc_span::sym;
26+
use rustc_span::{sym, Span};
27+
use rustc_trait_selection::traits::ObligationCtxt;
2628
use variable_check::{
2729
check_implements_par_iter, check_trait_impl, check_variables, generate_suggestion,
2830
is_type_valid,
@@ -54,21 +56,22 @@ dylint_linting::declare_late_lint! {
5456
impl<'tcx> LateLintPass<'tcx> for ParIter {
5557
// TODO: implement check crate to check if rayon is present
5658
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
57-
if let hir::ExprKind::MethodCall(path, recv, _args, _span) = &expr.kind
59+
if let hir::ExprKind::MethodCall(path, recv, _args, span) = &expr.kind
5860
&& let Some(suggestion) = generate_suggestion(cx, expr, path)
5961
{
6062
let ty = cx.typeck_results().expr_ty(recv);
61-
6263
let par_iter_traits = check_implements_par_iter(cx, recv);
6364
if !par_iter_traits.is_empty() && is_type_valid(cx, ty) {
6465
// TODO: issue with into_par_iter() need to check directly with
6566
// parallel iterator
66-
67+
//
6768
let mut allowed_methods: FxHashSet<&str> =
6869
["into_iter", "iter", "iter_mut", "map_or"]
6970
.into_iter()
7071
.collect();
71-
allowed_methods.extend(get_methods(cx));
72+
for par_iter_trait in par_iter_traits {
73+
allowed_methods.extend(get_methods(cx, par_iter_trait, ty, span));
74+
}
7275

7376
let mut top_expr = *recv;
7477
let mut found_iter_method = false;
@@ -164,25 +167,35 @@ impl<'a, 'tcx> hir::intravisit::Visitor<'_> for Validator<'a, 'tcx> {
164167
walk_expr(self, ex)
165168
}
166169
}
167-
168-
fn get_methods<'tcx>(cx: &LateContext<'tcx>) -> Vec<&'tcx str> {
169-
let mut res = Vec::new();
170-
if let (Some(parallel_iterator_def_id), Some(parallel_indexed_iterator_def_id)) = (
171-
get_trait_def_id(cx, &["rayon", "iter", "ParallelIterator"]),
172-
get_trait_def_id(cx, &["rayon", "iter", "IndexedParallelIterator"]),
173-
) {
174-
let ids = &[parallel_iterator_def_id, parallel_indexed_iterator_def_id];
175-
for def_id in ids {
176-
let associated_items = cx.tcx.associated_items(def_id);
177-
// Filter out only methods from the associated items
178-
let methods: Vec<&str> = associated_items
179-
.in_definition_order()
180-
.filter(|item| matches!(item.kind, ty::AssocKind::Fn))
181-
.map(|item| item.name.as_str())
182-
.collect();
183-
res.extend(methods);
184-
}
170+
use clippy_utils::ty::make_normalized_projection;
171+
172+
fn get_methods<'tcx>(
173+
cx: &LateContext<'tcx>,
174+
trait_def_id: hir::def_id::DefId,
175+
original_ty: rustc_middle::ty::Ty,
176+
span: Span,
177+
) -> Vec<&'tcx str> {
178+
let res = Vec::new();
179+
180+
let tcx = cx.tcx;
181+
let infcx = tcx.infer_ctxt().build();
182+
let origin = TypeVariableOrigin {
183+
kind: TypeVariableOriginKind::TypeInference,
184+
span,
185+
};
186+
let ty_var = infcx.next_ty_var(origin);
187+
if let Some(param_env) =
188+
// FIXME: what is assoc_ty
189+
if let Some(projection) =
190+
make_normalized_projection(tcx, param_env, trait_def_id, sym::Item, vec![])
191+
{
192+
let ocx = ObligationCtxt::new(&infcx);
193+
194+
// FIXME: what is obligation
195+
ocx.register_obligation(obligation);
196+
let some_errors = ocx.select_where_possible();
185197
}
198+
186199
res
187200
}
188201

0 commit comments

Comments
 (0)