mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-11 14:33:32 +00:00
unnecessary_sort_by
only warns if argument impl Ord
trait
This commit is contained in:
parent
4996e17b14
commit
ec5071931e
@ -1,6 +1,6 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::ty::is_type_diagnostic_item;
|
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{Expr, ExprKind, Mutability, Param, Pat, PatKind, Path, PathSegment, QPath};
|
use rustc_hir::{Expr, ExprKind, Mutability, Param, Pat, PatKind, Path, PathSegment, QPath};
|
||||||
@ -193,10 +193,15 @@ fn detect_lint(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<LintTrigger> {
|
|||||||
let vec_name = Sugg::hir(cx, &args[0], "..").to_string();
|
let vec_name = Sugg::hir(cx, &args[0], "..").to_string();
|
||||||
let unstable = name == "sort_unstable_by";
|
let unstable = name == "sort_unstable_by";
|
||||||
|
|
||||||
|
if_chain! {
|
||||||
if let ExprKind::Path(QPath::Resolved(_, Path {
|
if let ExprKind::Path(QPath::Resolved(_, Path {
|
||||||
segments: [PathSegment { ident: left_name, .. }], ..
|
segments: [PathSegment { ident: left_name, .. }], ..
|
||||||
})) = &left_expr.kind {
|
})) = &left_expr.kind;
|
||||||
if left_name == left_ident {
|
if left_name == left_ident;
|
||||||
|
if cx.tcx.get_diagnostic_item(sym::Ord).map_or(false, |id| {
|
||||||
|
implements_trait(cx, cx.typeck_results().expr_ty(left_expr), id, &[])
|
||||||
|
});
|
||||||
|
then {
|
||||||
return Some(LintTrigger::Sort(SortDetection { vec_name, unstable }));
|
return Some(LintTrigger::Sort(SortDetection { vec_name, unstable }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user