ruff
111bbc61 - [`refurb`] New rule to suggest min/max over sorted() (`FURB192`) (#10868)

Commit
1 year ago
[`refurb`] New rule to suggest min/max over sorted() (`FURB192`) (#10868) ## Summary Fixes #10463 Add `FURB192` which detects violations like this: ```python # Bad a = sorted(l)[0] # Good a = min(l) ``` There is a caveat that @Skylion007 has pointed out, which is that violations with `reverse=True` technically aren't compatible with this change, in the edge case where the unstable behavior is intended. For example: ```python from operator import itemgetter data = [('red', 1), ('blue', 1), ('red', 2), ('blue', 2)] min(data, key=itemgetter(0)) # ('blue', 1) sorted(data, key=itemgetter(0))[0] # ('blue', 1) sorted(data, key=itemgetter(0), reverse=True)[-1] # ('blue, 2') ``` This seems like a rare edge case, but I can make the `reverse=True` fixes unsafe if that's best. ## Test Plan This is unit tested. ## References https://github.com/dosisod/refurb/pull/333/files --------- Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
Parents
  • crates/ruff_linter
    • resources/test/fixtures/refurb
      • File
        FURB192.py
    • src
      • checkers/ast/analyze
        • File
          expression.rs
      • File
        codes.rs
      • rules/refurb
        • File
          mod.rs
        • rules
          • File
            mod.rs
          • File
            sorted_min_max.rs
        • snapshots
          • ruff_linter__rules__refurb__tests__FURB192_FURB192.py.snap
  • File
    ruff.schema.json