oppiliappan / statix

lints and suggestions for the nix programming language
https://git.peppe.rs/languages/statix/about
MIT License
552 stars 21 forks source link

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

Open 9999years opened 5 months ago

9999years commented 5 months ago

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 commented 5 months ago

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;
}