clippy_utils::diagnostics

Function span_lint_and_sugg

Source
pub fn span_lint_and_sugg<T: LintContext>(
    cx: &T,
    lint: &'static Lint,
    sp: Span,
    msg: impl Into<DiagMessage>,
    help: impl Into<SubdiagMessage>,
    sugg: String,
    applicability: Applicability,
)
Expand description

Add a span lint with a suggestion on how to fix it.

These suggestions can be parsed by rustfix to allow it to automatically fix your code. In the example below, help is "try" and sugg is the suggested replacement ".any(|x| x > 2)".

If you change the signature, remember to update the internal lint CollapsibleCalls

NOTE: Lint emissions are always bound to a node in the HIR, which is used to determine the lint level. For the span_lint_and_sugg function, the node that was passed into the LintPass::check_* function is used.

If you’re emitting the lint at the span of a different node than the one provided by the LintPass::check_* function, consider using span_lint_hir_and_then instead. This is needed for #[allow] and #[expect] attributes to work on the node highlighted in the displayed warning.

If you’re unsure which function you should use, you can test if the #[allow] attribute works where you would expect it to. If it doesn’t, you likely need to use span_lint_hir_and_then instead.

§Example

error: This `.fold` can be more succinctly expressed as `.any`
--> tests/ui/methods.rs:390:13
    |
390 |     let _ = (0..3).fold(false, |acc, x| acc || x > 2);
    |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.any(|x| x > 2)`
    |
    = note: `-D fold-any` implied by `-D warnings`