Issue #124 resolved

Inspection false flags

Anonymous created an issue

Inspections are good, but false flags, make them less useful. Here is a list of common false flags (IMHO.).

variable not assigned

case 1.

--example of a false flag
local string = string --2nd string gives, variable not assigned

This is correct behavior but it would be nice to be able to define expected global variables inline. This way the inspection can still be activated and helpful.

--@global table, string, tostring
local string = string 
local table = table

unused assignment

case 1.

--example of a false flag
local a, b -- unused assignment a, b
a = 1
b = 2

The unused assignment, should not be a warning if...

  1. The assignment is to the nil value/type and

  2. Is eventually set to something else

case 2.

--example of a false flag
local t= {'a', 'b', 'c'}
for i, v in ipairs(t) do -- unused assignment? i is used in the stateless iterator 

The variable i is used in the stateless iterator. Perhaps the control variable/return value a for loop statement can be an exception for this inspection. I have seen people do...

local t= {'a', 'b', 'c'}
for _, v in ipairs(t) do -- not unused because variable is named '_' 

perhaps '_' can be a special name that is ignored by the unused inspection in 'for .. in'

case 3.

local a
    a = 1  -- unused assignment? a is not recognised as an upvalue
return a

self explanatory...

Comments (13)

  1. Jon Akhtar repo owner

    The unused assignment bug only applies to initializations in a local statement. It is a bug that is due to some implementation reuse with the assignment statement, since nil assignments do matter there.

    I like the _ suggestion.

    I think 3 is a new bug related to my 1/2 complete rework of variable binding when you reuse a local variable by assigning to it again it becomes a new variable, but does not replace the old one in every case through the end of its scope.

  2. Jon Akhtar repo owner

    If you have your Lua SDK set up, and you are getting string as an undefined global - then it is a bug I have been seeing. string should always be defined. since it is defined in the SDK. If you don't have an SDK set up we'll have to figure out the best way around that.

  3. raynard_brennan

    I am doing web development and purchased Webstorm. but it turns out Webstorm doesn't have all the functionality/menus of the community edition/intellij for setting up projects. I got it working (once) by manually editing my project xml files but it the settings weren't completely portable over the git repo? So I gave up and cached all the globals I wanted to use at the top of my modules.

    "--example of a false flag local a, b -- unused assignment a, b a = 1 b = 2"

    This isn't a problem for me anymore, I read somewhere that this does or can actually slow things down (of course not even noticeably).

  4. raynard_brennan

    It just occurred to me that there would be a very nice way of getting around my no sdk problem. Considering that the functions for looking up documentation and globals are defined by dynamically loaded scripts. Could you just look for a direction in comments to find these functions

    for example

    --@doclua file://home/raynard/myapi.doclua

    OR then the huge advantage here

    --@doclua :-)

    This would be very useful to me.

  5. Aadvert

    Here's another one, though similar to case 2 in OP:

    local data = {"a", "b", a = 2, b = 1 }
    function mergeto(from, to)
      for k, v in pairs(from) do
        to[k] = v
    local data2 = {c = "some stuff is already here" }
    mergeto(data, data2)

    The code above, in IDEA 12.1.4, with Lua for IDEA. 'to' is marked as un-used in line 3, and 'k' is marked as unused in line 5.

  6. Log in to comment