Skip to content

Commit

Permalink
non-functional checkpoint commit, convert to diagnostic items
Browse files Browse the repository at this point in the history
  • Loading branch information
anall committed Feb 26, 2021
1 parent d2a091b commit 1467261
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
8 changes: 4 additions & 4 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1698,10 +1698,10 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
["ok_or_else", ..] => unnecessary_lazy_eval::lint(cx, expr, arg_lists[0], "ok_or"),
["collect", "map"] => lint_map_collect(cx, expr, arg_lists[1], arg_lists[0]),
["for_each", "inspect"] => inspect_for_each::lint(cx, expr, method_spans[1]),
["to_owned", ..] => owned_to_owned::check(cx, expr, Some(&paths::TO_OWNED), None),
["to_os_string", ..] => owned_to_owned::check(cx, expr, None, Some(&paths::OS_STRING)),
["to_path_buf", ..] => owned_to_owned::check(cx, expr, None, Some(&paths::PATH_BUF)),
["to_vec", ..] => owned_to_owned::check(cx, expr, None, Some(&paths::VEC)),
["to_owned", ..] => owned_to_owned::check(cx, expr, sym::ToOwned, None),
["to_os_string", ..] => owned_to_owned::check(cx, expr, sym::OsStr, Some(sym::OsString)),
["to_path_buf", ..] => owned_to_owned::check(cx, expr, sym::Path, Some(sym::PathBuf)),
["to_vec", ..] => owned_to_owned::check(cx, expr, sym::slice, Some(sym::Vec)),
_ => {},
}

Expand Down
35 changes: 30 additions & 5 deletions clippy_lints/src/methods/owned_to_owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,49 @@ use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::ExprKind;
use rustc_lint::LateContext;
use rustc_middle::ty::TyS;
use rustc_middle::ty::{TyS,ImplContainer,TraitContainer};
use rustc_span::symbol::Symbol;
use rustc_hir::Expr;

use super::OWNED_TO_OWNED;
use clippy_utils::is_type_diagnostic_item;

// local for now while I'm massively debugging this
fn match_diagnostic(cx: &LateContext<'_>, expr: &Expr<'_>, diag_item: Symbol) -> bool {
let def_id = cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
let associated = cx.tcx.opt_associated_item(def_id).and_then(|associated_item| match associated_item.container {
TraitContainer(assoc_def_id) => Some(assoc_def_id),
ImplContainer(assoc_def_id) => Some(assoc_def_id),
});
if let Some(assoc_def_id) = associated {
// Except this doesn't work for std::path::Path::to_path_buf as this print returns;
// got associated DefId(1:4405 ~ std[e437]::path::{impl#61}) for DefId(1:4413 ~ std[e437]::path::{impl#61}::to_path_buf) // kind : "std::path::Path"
// obviously def_path_str realizes this is in std::path::Path, but the assoc_id is "std[e437]::path::{impl#61}"
// how do I get from ::{impl#61} to ::Path (which has my diagnostic item)
println!("got associated {:?} for {:?} // kind : {:?}",assoc_def_id,def_id,cx.tcx.def_path_str(assoc_def_id));
println!(" --- {:?} {}",cx.tcx.get_diagnostic_item(diag_item), cx.tcx.get_diagnostic_item(diag_item) == Some(assoc_def_id));

cx.tcx.is_diagnostic_item(diag_item, assoc_def_id)
} else {
println!("got associated NONE for {:?}",def_id);
false
}
}

pub fn check(
cx: &LateContext<'_>,
expr: &hir::Expr<'_>,
trait_path: Option<&[&str]>,
expected_type_path: Option<&[&str]>,
trait_diagnostic: Symbol,
expected_type_diagnostic: Option<Symbol>,
) {
if_chain! {
if let ExprKind::MethodCall(method_path, _, [arg], _) = &expr.kind;
let return_type = cx.typeck_results().expr_ty(&expr);
let input_type = cx.typeck_results().expr_ty(arg);
if let Some(ty_name) = input_type.ty_adt_def().map(|adt_def| cx.tcx.item_name(adt_def.did) );
if TyS::same_type(return_type, input_type);
if trait_path.map_or(true, |path| match_trait_method(cx, expr, path)) &&
expected_type_path.map_or(true, |path| match_type(cx, input_type, path));
if match_diagnostic(cx, expr, trait_diagnostic) &&
expected_type_diagnostic.map_or(true, |diagnostic| is_type_diagnostic_item(cx,input_type,diagnostic));
then {
span_lint_and_sugg(
cx,OWNED_TO_OWNED,method_path.ident.span,
Expand Down

0 comments on commit 1467261

Please sign in to comment.