Difference between revisions of "sstrict.lua"

(Created page with "Super Strict is a pure Lua library that finds undeclared variables and other minor mistakes in your source code through static analysis. You do not need to execute any code to...")
 
(Documentation and source code)
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
Super Strict is a pure Lua library that finds undeclared variables and other minor mistakes in your source code through static analysis. You do not need to execute any code to find your mistakes: Super Strict will check the code during loading. Just include the "sstrict.lua" file and any subsequent calls to "require","dofile","loadfile" or "loadstring" will be checked through Super Strict. To exclude a specific Lua file from the being checked just place the "--!strict" line at the top of your source code.
+
Super Strict is a pure Lua library that finds undeclared variables and other minor mistakes in your source code through static analysis. You do not need to execute any code to find your mistakes because Super Strict will check the code during loading. Just require the "sstrict.lua" file and any subsequent calls to "require","dofile","loadfile" or "loadstring" will be checked through Super Strict.
 +
<source lang="lua">require('sstrict.lua')</source>
 +
To exclude a specific Lua file from the being checked place the "--!strict" line at the top of your source code.
  
sstrict.lua is not related in any way to the strict.lua library
+
== Undefined and unused variables ==
 +
<source lang="lua">function foo()
 +
  a = 5 -- undefined variable 'a'
 +
end
 +
function bar(a, b)
 +
  local c = a + b -- unused variable 'c'
 +
  return a + b
 +
end</source>
  
 +
== Redefinition of variable names ==
 +
<source lang="lua">for i = 1, 10 do
 +
  for i = 1, 10 do
 +
    -- variable name 'i' redefinition
 +
  end
 +
end</source>
 +
 +
== Empty and unnecessary code blocks ==
 +
<source lang="lua">for i = 1, 3 do
 +
  -- empty code block error
 +
end
 +
function baz()
 +
  local n = 5
 +
  n = n - 1 -- unnecessary code block error
 +
end</source>
 +
 +
== Constant conditions ==
 +
<source lang="lua">if 5+5 > 11 then
 +
  -- constant condition error in if/else statement
 +
end</source>
 +
 +
== Too many values in assignment ==
 +
<source lang="lua">a = 1, 2 -- too many values on the right-hand side in assignment</source>
 +
 +
== Duplicate variables, arguments and table fields ==
 +
<source lang="lua">t =
 +
{
 +
  ['a'] = 100,
 +
  a = 100 -- duplicate field 'a' in table constructor
 +
}
 +
for b, b in pairs(t) do
 +
  -- duplicate lvariable 'b'
 +
end
 +
c, c = 1, 2 -- duplicate variable 'c'
 +
function foo(d, d)
 +
  -- duplicate argument 'd'
 +
end</source>
 +
 +
== Documentation and source code ==
 
https://github.com/2dengine/sstrict.lua
 
https://github.com/2dengine/sstrict.lua
  
https://love2d.org/forums/viewtopic.php?t=90074
+
https://2dengine.com/doc/sstrict.html
  
 
{{#set:LOVE Version=Any}}
 
{{#set:LOVE Version=Any}}
 
{{#set:Description=Parses and detects mistakes in your Lua source code}}
 
{{#set:Description=Parses and detects mistakes in your Lua source code}}
{{#set:Keyword=Static Analysis}}
+
{{#set:Keyword=Debug}}
 
[[Category:Libraries]]
 
[[Category:Libraries]]

Latest revision as of 15:01, 15 October 2024

Super Strict is a pure Lua library that finds undeclared variables and other minor mistakes in your source code through static analysis. You do not need to execute any code to find your mistakes because Super Strict will check the code during loading. Just require the "sstrict.lua" file and any subsequent calls to "require","dofile","loadfile" or "loadstring" will be checked through Super Strict.

require('sstrict.lua')

To exclude a specific Lua file from the being checked place the "--!strict" line at the top of your source code.

Undefined and unused variables

function foo()
  a = 5 -- undefined variable 'a'
end
function bar(a, b)
  local c = a + b -- unused variable 'c'
  return a + b
end

Redefinition of variable names

for i = 1, 10 do
  for i = 1, 10 do
    -- variable name 'i' redefinition
  end
end

Empty and unnecessary code blocks

for i = 1, 3 do
  -- empty code block error
end
function baz()
  local n = 5
  n = n - 1 -- unnecessary code block error
end

Constant conditions

if 5+5 > 11 then
  -- constant condition error in if/else statement
end

Too many values in assignment

a = 1, 2 -- too many values on the right-hand side in assignment

Duplicate variables, arguments and table fields

t =
{ 
  ['a'] = 100,
  a = 100 -- duplicate field 'a' in table constructor
}
for b, b in pairs(t) do
  -- duplicate lvariable 'b'
end
c, c = 1, 2 -- duplicate variable 'c'
function foo(d, d)
  -- duplicate argument 'd'
end

Documentation and source code

https://github.com/2dengine/sstrict.lua

https://2dengine.com/doc/sstrict.html