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

redundant_pattern_bind is incorrect #64

Open tazjin opened 1 year ago

tazjin commented 1 year ago

Empty pattern bindings assert types, consider:

let
  crudeAssertAttrs = x @ { ... }: x;
in crudeAssertAttrs 42

=> fails to evaluate (`42` is not a set)

statix wants to reduce this to:

let
  crudeAssertAttrs = x: x;
in crudeAssertAttrs 42

which now evaluates to 42. This could allow an unexpected value to accidentally pass further than expected and introduce subtle bugs.

I initially thought this might be possible to correctly implement in some cases, for example if the body of a function does something with the argument that ensures it is an attrset (e.g. an attrset-related operator).

However, doing that correctly requires branch analysis and probably full runtime information, so now I think this check is impossible to do lexically, for example:

let
  something = ...;
  f = x @ { ... }: if something then x else x.key;
in
...