statix icon indicating copy to clipboard operation
statix copied to clipboard

W04 creates multiple redundant `inherit` statements (`Assignment instead of inherit from`)

Open 9999years opened this issue 1 year ago • 1 comments

With this code:

let
  config = {
    host = "host";
    user = "user";
    port = 22;
  };
in {
  host = config.host;
  user = config.user;
  port = config.port;
}

statix correctly identifies that these assignments could be inherit statements instead:

$ statix check inherit-repeat.nix
[W04] Warning: Assignment instead of inherit from
   ╭─[inherit-repeat.nix:8:3]
   │
 8 │   host = config.host;
   ·   ─────────┬─────────
   ·            ╰─────────── This assignment is better written with inherit
───╯
[W04] Warning: Assignment instead of inherit from
   ╭─[inherit-repeat.nix:9:3]
   │
 9 │   user = config.user;
   ·   ─────────┬─────────
   ·            ╰─────────── This assignment is better written with inherit
───╯
[W04] Warning: Assignment instead of inherit from
    ╭─[inherit-repeat.nix:10:3]
    │
 10 │   port = config.port;
    ·   ─────────┬─────────
    ·            ╰─────────── This assignment is better written with inherit
────╯

But it creates three inherit statements when I run statix fix, rather than one:

let
  config = {
    host = "host";
    user = "user";
    port = 22;
  };
in {
  inherit (config) host;
  inherit (config) user;
  inherit (config) port;
}

Expected

let
  config = {
    host = "host";
    user = "user";
    port = 22;
  };
in {
  inherit (config) host user port;
}

Version

$ statix --version
statix 0.5.8

9999years avatar Mar 04 '24 18:03 9999years

Similarly, if there already exists an inherit statement, the added inherits aren't combined into it:

let
  config = {
    host = "host";
    user = "user";
    port = 22;
    options = "";
  };
in {
  inherit (config) options;
  host = config.host;
  user = config.user;
  port = config.port;
}

9999years avatar Mar 04 '24 18:03 9999years