Class: Pedant::CheckConditionalOrLoopIsEmpty

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

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



29
30
31
# File 'lib/pedant/checks/conditional_or_loop_is_empty.rb', line 29

def self.requires
  super + [:trees]
end

Instance Method Details

#check(file, tree) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pedant/checks/conditional_or_loop_is_empty.rb', line 33

def check(file, tree)
  # All of the loops have a body attribute, so they can be checked together.
  [:For, :Foreach, :Repeat, :While].each do |cls|
    tree.all(cls).each do |node|
      next unless node.body.is_a? Nasl::Empty

      fail

      report(:error, "#{cls} loop in #{file} has an empty statement as its body.")
      report(:error, node.body.context(node))
    end
  end

  # An If statement may has two branches, each of which need to be checked.
  # This will not cause false positives on If statements without else
  # clauses, because those branches will be nil.
  tree.all(:If).each do |node|
    [:true, :false].each do |name|
      branch = node.send(name)

      next if branch.nil?
      next unless branch.is_a? Nasl::Empty

      fail

      report(:error, "If statement in #{file} has an empty statement as #{name} branch.")
      report(:error, branch.context(node))
    end
  end
end

#runObject



64
65
66
67
68
69
70
# File 'lib/pedant/checks/conditional_or_loop_is_empty.rb', line 64

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