Class: Pedant::CheckArityOfBuiltins

Inherits:
Check
  • Object
show all
Defined in:
lib/pedant/checks/arity_of_builtins.rb

Constant Summary collapse

@@anon_arity_of_one =
Set.new [
  "isnull",
  # Sockets
  "get_port_state",
  "get_tcp_port_state",
  "get_udp_port_state",
  "close",
  # String functions
  "strlen",
  "int",
  "uint",
  "chomp",
  "ord",
  "hex",
  "hexstr",
  "tolower",
  "toupper",
  "xmlparse",
  # Time
  "usleep",
  "sleep",
  # Arrays and lists
  "keys",
  "max_index",
  "sort",
  # Runtime checks
  "typeof",
  "typeof_ex",
  "defined_func",
  # Bignum functions
  "bn_dec2raw",
  "bn_raw2dec",
  "bn_hex2raw",
  "bn_raw2hex",
  "bn_sqr",
  # File system
  "fread",
  "unlink",
  "readdir",
  "mkdir",
  "rmdir",
  # Unkeyed hashes (crypto)
  "SHA",
  "SHA1",
  "SHA224",
  "SHA256",
  "SHA384",
  "SHA512",
  "RIPEMD160",
  "MD2",
  "MD4",
  "MD5",
  # Knowledge base
  "get_kb_item",
  "get_kb_list",
  "get_global_kb_item",
  "get_global_kb_list",
]

Instance Attribute Summary

Attributes inherited from Check

#result

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Check

all, depends, #fail, #fatal, friendly_name, inherited, #initialize, initialize!, list, #pass, provides, ready?, #report, run_checks_in_dependency_order, #skip, #warn

Constructor Details

This class inherits a constructor from Pedant::Check

Class Method Details

.requiresObject



90
91
92
# File 'lib/pedant/checks/arity_of_builtins.rb', line 90

def self.requires
  super + [:trees]
end

Instance Method Details

#check(file, tree) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pedant/checks/arity_of_builtins.rb', line 94

def check(file, tree)
  tree.all(:Call).each do |call|
    next unless call.name.indexes == []
    name = call.name.ident.name

    if @@anon_arity_of_one.include? name
      next if call.args.length == 1 and call.args.first.type == :anonymous
      fail
      report(:error, "The builtin function '#{name}' takes a single anonymous argument.")
      # Pick the right thing to highlight.
      if call.args.length == 0
        report(:error, call.context(call))
      elsif call.args.first.type != :anonymous
        report(:error, call.args[0].context(call))
      elsif call.args.length > 1
        report(:error, call.args[1].context(call))
      end
    end

    if name == "make_array"
      next if call.args.length.even?
      fail
      report(:error, "The builtin function 'make_array()' takes an even number of arguments.")
      report(:error, call.context(call))
    end
  end
end

#runObject



122
123
124
125
126
127
128
# File 'lib/pedant/checks/arity_of_builtins.rb', line 122

def run
  # This check will pass by default.
  pass

  # Run this check on the tree from every file.
  @kb[:trees].each { |file, tree| check(file, tree) }
end