/// The three "kinds" of sorts
enum SortingKind {
Vanilla,
- // The other kinds of lint are currently commented out because they
- // can map distinct values to equal ones. If the key function is
- // provably one-to-one, or if the Cmp function conserves equality,
- // then they could be linted on, but I don't know if we can check
- // for that.
+ /* The other kinds of lint are currently commented out because they
+ * can map distinct values to equal ones. If the key function is
+ * provably one-to-one, or if the Cmp function conserves equality,
+ * then they could be linted on, but I don't know if we can check
+ * for that. */
- // ByKey,
- // ByCmp,
+ /* ByKey,
+ * ByCmp, */
}
impl SortingKind {
/// The name of the stable version of this kind of sort
fn stable_name(&self) -> &str {
match self {
SortingKind::Vanilla => "sort",
- // SortingKind::ByKey => "sort_by_key",
- // SortingKind::ByCmp => "sort_by",
+ /* SortingKind::ByKey => "sort_by_key",
+ * SortingKind::ByCmp => "sort_by", */
}
}
/// The name of the unstable version of this kind of sort
fn unstable_name(&self) -> &str {
match self {
SortingKind::Vanilla => "sort_unstable",
- // SortingKind::ByKey => "sort_unstable_by_key",
- // SortingKind::ByCmp => "sort_unstable_by",
+ /* SortingKind::ByKey => "sort_unstable_by_key",
+ * SortingKind::ByCmp => "sort_unstable_by", */
}
}
/// Takes the name of a function call and returns the kind of sort
slice_name: String,
method: SortingKind,
method_args: String,
+ slice_type: String,
}
fn detect_stable_sort_primitive(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<LintDetection> {
if let ExprKind::MethodCall(method_name, _, args, _) = &expr.kind;
if let Some(slice) = &args.get(0);
if let Some(method) = SortingKind::from_stable_name(&method_name.ident.name.as_str());
- if is_slice_of_primitives(cx, slice);
+ if let Some(slice_type) = is_slice_of_primitives(cx, slice);
then {
let args_str = args.iter().skip(1).map(|arg| Sugg::hir(cx, arg, "..").to_string()).collect::<Vec<String>>().join(", ");
- Some(LintDetection { slice_name: Sugg::hir(cx, slice, "..").to_string(), method, method_args: args_str })
+ Some(LintDetection { slice_name: Sugg::hir(cx, slice, "..").to_string(), method, method_args: args_str, slice_type })
} else {
None
}
STABLE_SORT_PRIMITIVE,
expr.span,
format!(
- "Use {} instead of {}",
+ "used {} instead of {} to sort primitive type `{}`",
+ detection.method.stable_name(),
detection.method.unstable_name(),
- detection.method.stable_name()
+ detection.slice_type,
)
.as_str(),
"try",