Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Perl-Critic CPAN distribution.

Report information
The Basics
Id: 86616
Status: open
Priority: 0/
Queue: Perl-Critic

People
Owner: Nobody in particular
Requestors: victor [...] vsespb.ru
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Enhancement: Policy to detect non-transitive comparison functions
Comparison operator, if used with sort, should be transitive http://en.wikipedia.org/wiki/Comparison_sort Any complex comparison function need a proof of transitivity (maybe unit tests). I am not sure how hard it will be to detect possibly non-transitive functions. Maybe throw a warning if left part of '<=>'/'cmp' expression differs much from right part of it (and if <=>/cmp inside sort {} block) ? for example OK: sort { norm($a) <=> norm($b) } @a OK: sort { $a <=> $b } @a OK: sort { ($a||999) <=> ($b||999) } @a Not OK: sort { ($a||999) <=> ($b//999) } @a Not ok: sort { $a->[0] && $b->[0] ? $a->[0] <=> $b->[0] : $a->[1] <=> $b->[1] } @a OK(Ignore): sort{ somefunc($a,$b) } @a This often used and does not contain bugs, not sure how to detect that, however. sort { $a->[0] <=> $b->[0] or $a->[1] <=> $b->[1] } @a
Subject: Re: [rt.cpan.org #86616] Enhancement: Policy to detect non-transitive comparison functions
Date: Mon, 1 Jul 2013 14:56:16 -0700
To: bug-Perl-Critic [...] rt.cpan.org
From: Jeffrey Ryan Thalhammer <jeff [...] imaginative-software.com>
On Jul 1, 2013, at 2:12 PM, Victor Efimov via RT wrote: Show quoted text
> Any complex comparison function need a proof of transitivity (maybe unit tests).
That's a great idea, but I think it would be pretty hard to implement. The best you could probably do is compare the expressions on either side of the comparison operator and make sure they only differ by either $a or $b. PPI can normalize all the whitespace so comparing two expressions isn't that hard. But it doesn't create an abstract syntax tree (i.e. it doesn't understand operator precedence) so it would be difficult to know exactly which expressions to compare. This would be a great research project though. It could open the door to doing more sophisticated analysis of other things. -Jeff