diff options
| author | Jacob Kiesel <jake@bitcrafters.co> | 2024-03-29 09:38:59 -0600 |
|---|---|---|
| committer | Jacob Kiesel <jake@bitcrafters.co> | 2024-03-29 09:38:59 -0600 |
| commit | 89588f41f8a96984601b0abc7f8eaacfa3b7da8f (patch) | |
| tree | a37e54050ab0703a6ac45667b3cedcff6c2f5d29 | |
| parent | 4d7d66462f1907d6cd0240c30636e01429fb8f89 (diff) | |
| download | rust-89588f41f8a96984601b0abc7f8eaacfa3b7da8f.tar.gz rust-89588f41f8a96984601b0abc7f8eaacfa3b7da8f.zip | |
Add limitations section, move check
| -rw-r--r-- | clippy_lints/src/manual_clamp.rs | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/clippy_lints/src/manual_clamp.rs b/clippy_lints/src/manual_clamp.rs index 59fc8948c2f..1eadc200bed 100644 --- a/clippy_lints/src/manual_clamp.rs +++ b/clippy_lints/src/manual_clamp.rs @@ -28,6 +28,11 @@ declare_clippy_lint! { /// ### Why is this bad? /// clamp is much shorter, easier to read, and doesn't use any control flow. /// + /// ### Limitations + /// + /// This lint will only trigger if max and min are known at compile time, and max is + /// greater than min. + /// /// ### Known issue(s) /// If the clamped variable is NaN this suggestion will cause the code to propagate NaN /// rather than returning either `max` or `min`. @@ -145,9 +150,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualClamp { .or_else(|| is_match_pattern(cx, expr)) .or_else(|| is_if_elseif_pattern(cx, expr)); if let Some(suggestion) = suggestion { - if suggestion.min_less_than_max(cx) { - emit_suggestion(cx, &suggestion); - } + maybe_emit_suggestion(cx, &suggestion); } } } @@ -157,15 +160,16 @@ impl<'tcx> LateLintPass<'tcx> for ManualClamp { return; } for suggestion in is_two_if_pattern(cx, block) { - if suggestion.min_less_than_max(cx) { - emit_suggestion(cx, &suggestion); - } + maybe_emit_suggestion(cx, &suggestion); } } extract_msrv_attr!(LateContext); } -fn emit_suggestion<'tcx>(cx: &LateContext<'tcx>, suggestion: &ClampSuggestion<'tcx>) { +fn maybe_emit_suggestion<'tcx>(cx: &LateContext<'tcx>, suggestion: &ClampSuggestion<'tcx>) { + if !suggestion.min_less_than_max(cx) { + return; + } let ClampSuggestion { params: InputMinMax { input, |
