From 0c76f280a8a82d4f80114b26fb8ee86637efaa37 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 28 Jun 2012 15:20:05 -0700
Subject: [PATCH] Add hlint warning for wrong use of 'length'

While removing split instances check from hcheck, I saw a wrong use of
'length': this should only be used when one needs the actual length of
the list, and not when one wants to check whether the list is empty or
not (in that case, 'null' should be used).

To prevent this in the future, let's add a hlint hint for it, and fix
the warning it generates in hbal.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Agata Murawska <agatamurawska@google.com>
---
 htools/Ganeti/HTools/Program/Hbal.hs | 2 +-
 htools/lint-hints.hs                 | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/htools/Ganeti/HTools/Program/Hbal.hs b/htools/Ganeti/HTools/Program/Hbal.hs
index 3a361628b..8c4136f16 100644
--- a/htools/Ganeti/HTools/Program/Hbal.hs
+++ b/htools/Ganeti/HTools/Program/Hbal.hs
@@ -328,7 +328,7 @@ checkGroup verbose gname nl il = do
              "Initial check done: %d bad nodes, %d bad instances.\n"
              (length bad_nodes) (length bad_instances)
 
-  when (length bad_nodes > 0) $
+  when (not (null bad_nodes)) $
          putStrLn "Cluster is not N+1 happy, continuing but no guarantee \
                   \that the cluster will end N+1 happy."
 
diff --git a/htools/lint-hints.hs b/htools/lint-hints.hs
index fd110f87d..8c9828a42 100644
--- a/htools/lint-hints.hs
+++ b/htools/lint-hints.hs
@@ -4,3 +4,8 @@ warn = map (\v -> (v, x)) y ==> zip y (repeat x)
   where _ = notIn v x
 warn = map (\v -> (x, v)) ==> zip (repeat x)
   where _ = notIn v x
+
+-- The following warn on use of length instead of null
+warn = length x > 0 ==> not (null x)
+warn = length x /= 0 ==> not (null x)
+warn = length x == 0 ==> null x
-- 
GitLab