Skip to content

Commit

Permalink
Warn about deeply nested negation
Browse files Browse the repository at this point in the history
  • Loading branch information
hurufu committed Oct 7, 2024
1 parent be3e011 commit 2d1ebf0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/lib/warnings.pl
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@
unsound_type_test(G),
functor(G, F, 1),
warn_fail("~q is a constant source of bugs, use ~a_si/1 from library(si)", [F/1,F]).

% Warn when more than 2 negations are nested. Double negation has legit
% use-case, but I don't think that more nested negations are ever useful.
%
user:goal_expansion(G, _) :-
nonvar(G),
G = (\+ \+ \+ _),
warn_fail("Nested negations can be reduced", []).
3 changes: 3 additions & 0 deletions src/tests/warnings.pl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
t :-
x; integer(_).

n :-
\+ \+ \+ foo(_).

x :-
a.
b,
Expand Down
3 changes: 2 additions & 1 deletion tests/scryer/cli/src_tests/warnings_tests.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
% Warning: integer/1 is a constant source of bugs, use integer_si/1 from library(si) at line 4 of warnings.pl
% Warning: (b,c) attempts to re-define (,)/2 at line 9 of warnings.pl
% Warning: Nested negations can be reduced at line 7 of warnings.pl
% Warning: (b,c) attempts to re-define (,)/2 at line 12 of warnings.pl

0 comments on commit 2d1ebf0

Please sign in to comment.