Skip to content

Commit ec50719

Browse files
committed
unnecessary_sort_by only warns if argument impl Ord trait
1 parent 4996e17 commit ec50719

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

clippy_lints/src/unnecessary_sort_by.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::sugg::Sugg;
3-
use clippy_utils::ty::is_type_diagnostic_item;
3+
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
44
use if_chain::if_chain;
55
use rustc_errors::Applicability;
66
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> {
193193
let vec_name = Sugg::hir(cx, &args[0], "..").to_string();
194194
let unstable = name == "sort_unstable_by";
195195

196+
if_chain! {
196197
if let ExprKind::Path(QPath::Resolved(_, Path {
197198
segments: [PathSegment { ident: left_name, .. }], ..
198-
})) = &left_expr.kind {
199-
if left_name == left_ident {
199+
})) = &left_expr.kind;
200+
if left_name == left_ident;
201+
if cx.tcx.get_diagnostic_item(sym::Ord).map_or(false, |id| {
202+
implements_trait(cx, cx.typeck_results().expr_ty(left_expr), id, &[])
203+
});
204+
then {
200205
return Some(LintTrigger::Sort(SortDetection { vec_name, unstable }));
201206
}
202207
}

0 commit comments

Comments
 (0)