]> gitweb.factorcode.org Git - factor.git/blobdiff - extra/lint/lint.factor
use reject instead of [ ... not ] filter.
[factor.git] / extra / lint / lint.factor
index fc43cefc5fcf8927e879e1415dd6550352b8ebb0..2d33c0352026d90307fbd1dbea9087c65ab846b4 100644 (file)
@@ -1,11 +1,12 @@
-! Copyright (C) 2007, 2008 Doug Coleman.
+! Copyright (C) 2007, 2008, 2011 Doug Coleman.
 ! See http://factorcode.org/license.txt for BSD license.
 
 USING: accessors alien arrays assocs classes
-classes.tuple.private combinators.short-circuit fry hashtables
-io kernel kernel.private locals.backend make math namespaces
-prettyprint quotations sequences sequences.deep shuffle
-slots.private vectors vocabs words words.alias ;
+classes.tuple.private combinators.short-circuit continuations
+fry hashtables io kernel kernel.private locals.backend make math
+math.private namespaces prettyprint quotations sequences
+sequences.deep shuffle slots.private splitting stack-checker
+vectors vocabs words words.alias ;
 
 IN: lint
 
@@ -18,7 +19,6 @@ CONSTANT: manual-substitutions
         { rot [ [ swap ] dip swap ] }
         { rot [ swapd swap ] }
         { over [ dup swap ] }
-        { tuck [ dup -rot ] }
         { swapd [ [ swap ] dip ] }
         { 2nip [ nip nip ] }
         { 2drop [ drop drop ] }
@@ -30,21 +30,23 @@ CONSTANT: manual-substitutions
 
 CONSTANT: trivial-defs
     {
-        [ drop t ] [ drop f ]
-        [ 2drop t ] [ 2drop f ]
-        [ 3drop t ] [ 3drop f ]
-        [ ">" write ] [ "/>" write ]
-        [ length 1 - ] [ length 1 = ] [ length 1 > ]
-        [ drop f f ] [ drop f t ] [ drop t f ] [ drop t t ]
-        [ 2drop f f ] [ 2drop f t ] [ 2drop t f ] [ 2drop t t ]
-        [ drop f f f ]
-        [ nip f f ]
+        [ ">" write ] [ "/>" write ] [ " " write ]
         [ 0 or + ]
-        [ dup 0 > ] [ dup 0 <= ] [ dup 0 < ]
-        [ over 0 > ] [ over 0 <= ] [ over 0 < ]
         [ dup length iota ]
         [ 0 swap copy ]
-        [ dup 1 + ] [ drop 1 + ]
+        [ dup length ]
+        [ 0 swap ]
+        [ 2dup = ] [ 2dup eq? ]
+        [ = not ] [ eq? not ]
+        [ boa throw ]
+        [ with each ] [ with map ]
+        [ curry filter ]
+        [ compose compose ]
+        [ empty? ] [ empty? not ]
+        [ dup empty? ] [ dup empty? not ]
+        [ 2dup both-fixnums? ]
+        [ [ drop ] prepose ]
+        [ 1 0 ? ]
     }
 
 : lintable-word? ( word -- ? )
@@ -60,7 +62,7 @@ CONSTANT: trivial-defs
 : ignore-def? ( def -- ? )
     {
         ! Remove small defs
-        [ length 2 <= ]
+        [ length 1 <= ]
 
         ! Remove trivial defs
         [ trivial-defs member? ]
@@ -71,9 +73,109 @@ CONSTANT: trivial-defs
         ! Remove words with locals
         [ [ \ load-locals = ] any? ]
 
+        ! Remove stuff with wrappers
+        [ [ wrapper? ] any? ]
+
+        ! Remove trivial math
+        [ [ { [ number? ] [ { + - / * /i /f >integer } member? ] } 1|| ] all? ]
+
+        ! Remove more trival defs
+        [
+            {
+                [ length 2 = ]
+                [ first2 [ word? ] either? ]
+                [ first2 [ { dip dup over swap drop } member? ] either? ]
+            } 1&&
+        ]
+
+        ! Remove [ V{ } clone ] and related
+        [
+            {
+                [ length 2 = ]
+                [ first { [ sequence? ] [ assoc? ] } 1|| ]
+                [ second { clone clone-like like assoc-like make } member? ]
+            } 1&&
+        ]
+
+        ! Remove [ foo get ] and related
+        [
+            {
+                [ length 2 = ]
+                [ first word? ]
+                [ second { get get-global , % } member? ]
+            } 1&&
+        ]
+
+        ! Remove [ first second ] and related
+        [
+            {
+                [ length 2 = ]
+                [ first { first second third } member? ]
+                [ second { first second third } member? ]
+            } 1&&
+        ]
+
+        ! Remove [ [ trivial ] if ] and related
+        [
+            {
+                [ length 2 = ]
+                [ first { [ quotation? ] [ ignore-def? ] } 1&& ]
+                [ second { if if* unless unless* when when* curry } member? ]
+            } 1&&
+        ]
+
+        ! Remove [ n - ] and related
+        [
+            {
+                [ length 2 = ]
+                [ first { [ number? ] [ boolean? ] } 1|| ]
+                [ second { + - / * < <= = >= > shift bitand bitor bitxor eq? } member? ]
+            } 1&&
+        ]
+
+        ! Remove [ dup 0 > ] and related
+        [
+            {
+                [ length 3 = ]
+                [ first { dup over } member? ]
+                [ second number? ]
+                [ third { + - / * < <= = >= > } member? ]
+            } 1&&
+        ]
+
+        ! Remove [ drop f f ] and related
+        [
+            {
+                [ length 4 <= ]
+                [ first { drop 2drop 3drop nip 2nip } member? ]
+                [ rest-slice [ boolean? ] all? ]
+            } 1&&
+        ]
+
+        ! Remove [ length 1 = ] and related
+        [
+            {
+                [ length 3 = ]
+                [ first \ length = ]
+                [ second number? ]
+                [ third { + - / * < <= = >= > } member? ]
+            } 1&&
+        ]
+
+        ! Remove [ dup length 1 = ] and related
+        [
+            {
+                [ length 4 = ]
+                [ first { dup over } member? ]
+                [ second \ length = ]
+                [ third number? ]
+                [ fourth { + - / * < <= = >= > } member? ]
+            } 1&&
+        ]
+
         ! Remove numbers/t/f only defs
         [
-            [ { [ number? ] [ t? ] [ f eq? ] } 1|| ] all?
+            [ { [ number? ] [ boolean? ] } 1|| ] all?
         ]
 
         ! Remove [ tag n eq? ]
@@ -84,13 +186,13 @@ CONSTANT: trivial-defs
             } 1&&
         ]
 
-        ! Remove [ { foo } declare class ]
+        ! Remove [ { foo } declare class-of ]
         [
             {
                 [ length 3 = ]
                 [ first { [ array? ] [ length 1 = ] } 1&& ]
                 [ second \ declare = ]
-                [ third \ class = ]
+                [ third \ class-of = ]
             } 1&&
         ]
 
@@ -111,11 +213,6 @@ CONSTANT: trivial-defs
                 [ third \ slot = ]
             } 1&&
         ]
-
-        ! Remove [ ... \ cdecl ]
-        [
-            { [ length 3 = ] [ last \ cdecl = ] } 1&&
-        ]
     } 1|| ;
 
 : all-callables ( def -- seq )
@@ -135,7 +232,7 @@ SYMBOL: lint-definitions-keys
     lintable-words load-definitions
 
     ! Remove words that are their own definition
-    [ [ [ def>> ] [ 1quotation ] bi = not ] filter ] assoc-map
+    [ [ [ def>> ] [ 1quotation ] bi = ] reject ] assoc-map
 
     ! Add manual definitions
     manual-substitutions over '[ _ push-at ] assoc-each
@@ -155,7 +252,7 @@ M: callable lint ( quot -- seq )
     [ lint-definitions-keys get-global ] dip '[ _ subseq? ] filter ;
 
 M: word lint ( word -- seq/f )
-    def>> all-callables [ lint ] map concat ;
+    def>> [ callable? ] deep-filter [ lint ] map concat ;
 
 : word-path. ( word -- )
     [ vocabulary>> write ":" write ] [ . ] bi ;
@@ -187,12 +284,32 @@ GENERIC: run-lint ( obj -- obj )
 
 M: sequence run-lint ( seq -- seq )
     [ dup lint ] { } map>assoc trim-self
-    [ second empty? not ] filter filter-symbols ;
+    [ second empty? ] reject filter-symbols ;
 
 M: word run-lint ( word -- seq ) 1array run-lint ;
 
 PRIVATE>
 
+: find-swap/swap ( word -- ? )
+    def>> [ callable? ] deep-filter
+    [
+        {
+            [ [ \ swap = ] count 2 >= ]
+            [
+                { swap } split rest but-last
+                [ [ infer ] [ 2drop ( -- ) ] recover ( x -- x ) = ] any?
+            ]
+        } 1&&
+    ] any? ;
+
+: find-redundant-word-props ( -- seq )
+    all-words [
+        {
+            [ { [ foldable? ] [ flushable? ] } 1|| ]
+            [ inline? ]
+        } 1&&
+    ] filter ;
+
 : lint-all ( -- seq )
     all-words run-lint dup lint. ;