-use crate::utils::{is_slice_of_primitives, span_lint_and_sugg, sugg::Sugg};
-
+use clippy_utils::diagnostics::span_lint_and_then;
+use clippy_utils::{is_slice_of_primitives, sugg::Sugg};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! {
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
declare_clippy_lint! {
/// When sorting primitive values (integers, bools, chars, as well
/// When sorting primitive values (integers, bools, chars, as well
- /// as arrays, slices, and tuples of such items), it is better to
+ /// as arrays, slices, and tuples of such items), it is typically better to
- /// **Why is this bad?**
- /// Using a stable sort consumes more memory and cpu cycles. Because
- /// values which compare equal are identical, preserving their
+ /// ### Why is this bad?
+ /// Typically, using a stable sort consumes more memory and cpu cycles.
+ /// Because values which compare equal are identical, preserving their
/// relative order (the guarantee that a stable sort provides) means
/// nothing, while the extra costs still apply.
///
/// relative order (the guarantee that a stable sort provides) means
/// nothing, while the extra costs still apply.
///
- if let ExprKind::MethodCall(method_name, _, args, _) = &expr.kind;
+ if let ExprKind::MethodCall(method_name, args, _) = &expr.kind;
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(", ");
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(", ");
impl LateLintPass<'_> for StableSortPrimitive {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
if let Some(detection) = detect_stable_sort_primitive(cx, expr) {
impl LateLintPass<'_> for StableSortPrimitive {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
if let Some(detection) = detect_stable_sort_primitive(cx, expr) {
- "try",
- format!(
- "{}.{}({})",
- detection.slice_name,
- detection.method.unstable_name(),
- detection.method_args
- ),
- Applicability::MachineApplicable,
+ |diag| {
+ diag.span_suggestion(
+ expr.span,
+ "try",
+ format!(
+ "{}.{}({})",
+ detection.slice_name,
+ detection.method.unstable_name(),
+ detection.method_args,
+ ),
+ Applicability::MachineApplicable,
+ );
+ diag.note(
+ "an unstable sort typically performs faster without any observable difference for this data type",
+ );
+ },