Class: RemLint::Rules::LiteralTypeMismatch

Inherits:
RemLint::Rule show all
Defined in:
lib/remlint/rules/literal_type_mismatch.rb

Overview

Comparisons between literals of different types.

compare() in src/expr.c settles both halves of this exactly:

if (v1.type != v2.type) {
  if (how == EQ)      ans->v.val = 0;
  else if (how == NE) ans->v.val = 1;
  else                return E_BAD_TYPE;
}

So "foo" == 14:33 is not a comparison that happens to be false today. It is the constant 0, on every date, for ever -- and Remind reports nothing, because as far as it is concerned the answer is simply no. An ordering operator across types at least errors, but only on the day the line is reached.

Both operands must be literals for the type to be knowable without running anything, which is the usual case for the mistake: comparing a variable against a constant of the wrong shape.

Constant Summary collapse

CONSTANT =
{ "==" => "0", "!=" => "1" }.freeze
ORDERING =
%w[< > <= >=].freeze
OPERATORS =
(CONSTANT.keys + ORDERING).freeze
BAD_TYPE =

E_BAD_TYPE's text, from src/err.h.

"Type mismatch"
DATETIME =

'2027-01-01', '14:33', '2027-01-01@14:33' -- src/expr.c reads a single-quoted literal as a DATE, a TIME or a DATETIME by its shape.

/\A'.*@.*'\z/
DATE =
%r{\A'\d{4}[-/]\d{1,2}[-/]\d{1,2}'\z}
TIME =
/\A'\d{1,2}[:.]\d{2}'\z/

Constants inherited from RemLint::Rule

RemLint::Rule::REGISTRY

Instance Attribute Summary

Attributes inherited from RemLint::Rule

#config, #document, #offenses

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RemLint::Rule

all, enabled_by_default?, find, inherited, #initialize, rule_name, #rule_name, #run

Constructor Details

This class inherits a constructor from RemLint::Rule

Class Method Details

.default_severityObject



41
42
43
# File 'lib/remlint/rules/literal_type_mismatch.rb', line 41

def self.default_severity
  "error"
end

.descriptionObject



45
46
47
# File 'lib/remlint/rules/literal_type_mismatch.rb', line 45

def self.description
  "A comparison between literals of different types."
end

Instance Method Details

#checkObject



49
50
51
52
53
54
55
# File 'lib/remlint/rules/literal_type_mismatch.rb', line 49

def check
  document.logical_lines.each_with_index do |logical_line, index|
    if document.commands[index].code?
      check_line(logical_line)
    end
  end
end