Languages

Tags

simplify-module-include
Redundancy OCaml

  • What it does

    Simplifies module includes

    Load Live Example
  • Match

    module :[module_define] = struct include :[[module_include]] end
    

    Rewrite

    module :[module_define] = :[module_include]
    
  • Example

    Input Source
    module That_queue : Queue_intf = struct
      include Linked_queue
    end
    
    Diff
    -module That_queue : Queue_intf = struct
    -  include Linked_queue
    -end
    +module That_queue : Queue_intf = Linked_queue
    
  • Author

    Anonymous

  • Known problems

    None

  • Id

    simplify-module-include-comby-ocaml-1000

control-flow-for-continue
Editor Redundancy Style C++

  • What it does

    Removes redundant continue statement in for loops.

    Load Live Example
  • Match

    for (:[1]) { continue; }
    

    Rewrite

    for (:[1]) {}
    
  • Example

    Input Source
    int main() {
      for (i = 0; i < 10; i++) { continue; }
    }
    
    Diff
     int main() {
    -  for (i = 0; i < 10; i++) { continue; }
    +  for (i = 0; i < 10; i++) {}
     }
    
  • Known problems

    None

  • Reference
  • Author

    Anonymous

  • Id

    clang-tidy-readability-redundant-control-flow-1000

replace-with-bytes-equal
Style Go

  • What it does

    Uses Equal instead of Compare for bytes

    Load Live Example
  • Match

    if bytes.Compare(:[1], :[2]) == 0 {:[3]}
    

    Rewrite

    if bytes.Equal(:[1], :[2]) {:[3]}
    
  • Example

    Input Source
    if bytes.Compare(x, y) == 0 {}
    
    Diff
    -if bytes.Compare(x, y) == 0 {}
    +if bytes.Equal(x, y) {}
    
  • Known problems

    None

  • Improvements

    Add reference implementation

  • Reference
  • Author

    Anonymous

  • Id

    staticcheck-replace-with-bytes-equal-1004

replace-with-string-contains
Style Go

  • What it does

    Uses Contains for string matching instead of checking index

    Load Live Example
  • Match

    if strings.Index(:[1], :[2]) != -1 {:[3]}
    

    Rewrite

    if strings.Contains(:[1], :[2]) {:[3]}
    
  • Example

    Input Source
    if strings.Index(x, y) != -1 {}
    
    Diff
    -if strings.Index(x, y) != -1 {}
    +if strings.Contains(x, y) {}
    
  • Known problems

    None

  • Improvements

    Add reference implementation

  • Reference
  • Author

    Anonymous

  • Id

    staticcheck-replace-with-string-contains-1003

redundant-nil-check-loop
Style Go

  • What it does

    Removes redundant nil checks.

    Load Live Example
  • Match

    if :[var_check] != nil {
      for :[defines] := range :[var_use] {:[inner_body]}
    }
    
    where :[var_check] == :[var_use]
    

    Rewrite

    for :[defines] := range :[var_use] {:[inner_body]}
    
  • Example

    Input Source
    func (HTTPRequest *HTTPRequest) run(client *Client) ([]byte, error) {
    	var err error
    	values := make(url.Values)
    	if HTTPRequest.Parameters != nil {
    		for k, v := range HTTPRequest.Parameters {
    			values.Set(k, fmt.Sprintf("%v", v))
    		}
    	}
    }
    
    Diff
     func (HTTPRequest *HTTPRequest) run(client *Client) ([]byte, error) {
     	var err error
     	values := make(url.Values)
    -	if HTTPRequest.Parameters != nil {
    -		for k, v := range HTTPRequest.Parameters {
    +	for k, v := range HTTPRequest.Parameters {
     			values.Set(k, fmt.Sprintf("%v", v))
     		}
    -	}
     }
    
  • Known problems

    This rule is only allowed on maps and slices, but may match on if statements that check pointers. Checks on pointers should not be removed.

  • Reference
  • Author

    Anonymous

  • Id

    staticcheck-replace-with-bytes-equal-1004

if-not
Style Clojure

  • What it does

    Simplifies if not conditionals to if-not

    Load Live Example
  • Match

    (if (not :[1]) :[2] :[3])
    

    Rewrite

    (if-not :[1] :[2] :[3])
    
  • Example

    Input Source
    '(if (not test) then else)
    
    Diff
    -'(if (not test) then else)
    +'(if-not test then else)
    
  • Known problems

    None

  • Reference
  • Author

    Anonymous

  • Id

    if-not-kibit-1001

when-test
Style Clojure

  • What it does

    Simplifies if conditionals to use when

    Load Live Example
  • Match

    (if :[1] :[2] nil)
    

    Rewrite

    (when :[1] :[2])
    
  • Example

    Input Source
    '(if test then nil)
    
    Diff
    -'(if test then nil)
    +'(when test then)
    
  • Known problems

    None

  • Reference
  • Author

    Anonymous

  • Id

    when-kibit-1002

assoc-assoc-to-assoc-in-get
Style Clojure

  • What it does

    Simplifies assoc calls to assoc-in

    Load Live Example
  • Match

    (assoc :[coll0] :[key0] (assoc (get :[coll1] :[key1]) :[key2] :[val]))
    
    where :[coll0] == :[coll1], :[key0] == :[key1]
    

    Rewrite

    (assoc-in :[coll0] [:[key0] :[key2]] :[val])
    
  • Example

    Input Source
    '(assoc coll k0 (assoc (get coll k0) k1 a))
    
    Diff
    -'(assoc coll k0 (assoc (get coll k0) k1 a))
    +'(assoc-in coll [k0 k1] a)
    
  • Known problems

    None

  • Reference
  • Author

    Anonymous

  • Id

    assoc-assoc-to-in-get-kibit-1000

use-where-type
Readability Dart

  • What it does

    Replaces where type checks with a more readable whereType

    Load Live Example
  • Match

    .where((:[var_bind]) => :[var_use] is :[[type]])
    
    where :[var_bind] == :[var_use]
    

    Rewrite

    .whereType<:[type]>()
    
  • Example

    Input Source
    iterable.where((e) => e is T)
    
    Diff
    -iterable.where((e) => e is T)
    +iterable.whereType<T>()
    
  • Known problems

    Only works if whereType is implemented.

  • Reference
  • Author

    Anonymous

  • Id

    effective-dart-where-type-1000

prefer-is-not-empty
Readability Dart

  • What it does

    Removes redundant continue statement in for loops.

    Load Live Example
  • Match

    if (!:[container].isEmpty)
    

    Rewrite

    if (:[container].isNotEmpty)
    
  • Example

    Input Source
    void main() {
        var todo = [];
        if (!todo.isEmpty) print('things to do...');
    }
    
    Diff
     void main() {
         var todo = [];
    -    if (!todo.isEmpty) print('things to do...');
    +    if (todo.isNotEmpty) print('things to do...');
     }
    
  • Known problems

    Only works for Iterable and Map instances.

  • Reference
  • Author

    Anonymous

  • Id

    effective-dart-use-is-not-empty-1000

prefer-is-empty
Performance Readability Dart

  • What it does

    Replaces length checks with a more readable and performant getters.

    Load Live Example
  • Match

    if (:[container].length != 0)
    

    Rewrite

    if (:[container].isNotEmpty)
    
  • Example

    Input Source
    void main() {
        var lunchBox = [];
        if (lunchBox.length != 0) print('ready to eat...');
    }
    
    Diff
     void main() {
         var lunchBox = [];
    -    if (lunchBox.length != 0) print('ready to eat...');
    +    if (lunchBox.isNotEmpty) print('ready to eat...');
     }
    
  • Known problems

    Only works if :[container] is a type implementing the isNotEmpty property.

  • Improvements

    Add an isEmpty variant.

  • Reference
  • Author

    Anonymous

  • Id

    effective-dart-prefer-is-empty-1000

redundant-field-names-single
Redundancy Rust

  • What it does

    Removes single redundant field names.

    Load Live Example
  • Match

    { :[[field]]: :[[use]] }
    
    where :[field] == :[use]
    

    Rewrite

    { :[field] }
    
  • Example

    Input Source
    let bar: u8 = 123;
    struct Foo {
        bar: u8,
    }
    let foo = Foo { bar: bar };
    
    Diff
     struct Foo {
         bar: u8,
     }
    -let foo = Foo { bar: bar };
    +let foo = Foo { bar };
    
  • Known problems

    None

  • Reference
  • Author

    Anonymous

  • Id

    redundant-field-names-1000

append
Readability Erlang

  • What it does

    Uses ++ infix operator instead of List.append

    Load Live Example
  • Match

    lists:append(:[1], :[2])
    

    Rewrite

    :[1] ++ :[2]
    
  • Example

    Input Source
    get_values(T, Key, lists:append(V, Acc)).
    
    Diff
    -get_values(T, Key, lists:append(V, Acc)).
    +get_values(T, Key, V ++ Acc).
    
  • Known problems

    None

  • Reference

    See erl-tidier described in this paper.

  • Author

    Anonymous

  • Id

    infix-append-erl-tidier-1000

auto-list-comp-simple-map
Readability Erlang

  • What it does

    Simplifies List.map to a list comprehension.

    Load Live Example
  • Match

    lists:map(fun :[fn_name]/1, :[list] end)
    

    Rewrite

    [:[fn_name](V1) || V1 <- :[list]]
    
  • Example

    Input Source
    lists:map(fun x/1, l end)
    
    Diff
    -lists:map(fun x/1, l end)
    +[x(V1) || V1 <- l]
    
  • Known problems

    None

  • Reference
  • Author

    Anonymous

  • Id

    auto-list-comp-erlang

simplify-piping
Readability Elm

  • What it does

    Simplifies a consecutive pipe left operation |> for map

    Load Live Example
  • Match

    |> :[[container1]].map :[[fn1]]
    |> :[[container2]].map :[[fn2]]
    
    where :[container1] == :[container2],
    match :[container1] {
    | "List" -> true
    | "Array" -> true
    | "Set" -> true
    }
    

    Rewrite

    |> :[container1].map (:[fn1] >> :[fn2])
    
  • Example

    Input Source
    typeRefs
            |> allRefs
            |> importsWithoutSelf apiSubmodule importingFrom
            |> List.map toModuleName
            |> List.map toImportString
            |> String.join "\n"
    
    Diff
     typeRefs
             |> allRefs
             |> importsWithoutSelf apiSubmodule importingFrom
    -        |> List.map toModuleName
    -        |> List.map toImportString
    +        |> List.map (toModuleName >> toImportString)
             |> String.join "\n"
    
  • Known problems

    None

  • Reference
  • Author

    Anonymous

  • Id

    elm-lint-simplify-pipe-left-1000

use-forall
Readability Scala

  • What it does

    Simplifies folds that checks a “for-all” condition.

    Load Live Example
  • Match

    .foldLeft(true)(:[1] && :[2])
    
    where :[1] == :[2]
    

    Rewrite

    .forall(identity)
    
  • Example

    Input Source
    val l = List(true, false, false, true)
    val andAll = l.foldLeft(true)(_ && _)
    
    Diff
     val l = List(true, false, false, true)
    -val andAll = l.foldLeft(true)(_ && _)
    +val andAll = l.forall(identity)
    
  • Known problems

    None

  • Reference
  • Author

    Anonymous

  • Id

    use-for-all-comby-scala-1000

filter-dot-size
Readability Scala

  • What it does

    Simplifies .filter(...).size call chains to use .count

    Load Live Example
  • Match

    .filter(:[1]).size
    

    Rewrite

    .count(:[1])
    
  • Example

    Input Source
    object Test {
      val list = List(1,2,3,4).filter(_ % 2 == 0).size
    }
    
    Diff
     object Test {
    -  val list = List(1,2,3,4).filter(_ % 2 == 0).size
    +  val list = List(1,2,3,4).count(_ % 2 == 0)
     }
    
  • Known problems

    None

  • Reference
  • Author

    Anonymous

  • Id

    count-scapegoat-scala-1000

Credits: this page is modified from the excellent rust-clippy page (GH)
Copyright © 2019