oppiliappan / statix

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

False-positive with nested attribute set in dot-notation #2

Closed sbruder closed 2 years ago

sbruder commented 2 years ago

First, thanks for the nice tool!

When I ran it on some of my nix files, I noticed that the lint W03 (Assignment instead of inherit) sometimes recommends to inherit when it is not possible.

For example running statix check on

let
  foo = "value";
in
{
  foo.bar = foo;
}

results in a warning

[W03] Warning: Assignment instead of inherit
   ╭─[test.nix:5:3]
   │
 5 │   foo.bar = foo;
   ·   ───────┬──────
   ·          ╰──────── This assignment is better written with inherit
───╯

and statix fix changes it to

let
  foo = "value";
in
{
  inherit foo;
}

However, the evaluation of those two files is not the same:

$ nix eval --json -f test.nix
{"foo":{"bar":"value"}}
$ nix eval --json -f after-fix.nix
{"foo":"value"}

This does not happen when the variable name to be inherited (in this example foo) is not the first token (e.g. in baz.foo.bar = foo;).

oppiliappan commented 2 years ago

Thanks for the detailed report, this should be fixed in the latest release (v0.2.4)!