Warn me when I set a value equal to itself

Today I wrote this code:

local result = nil
local connection
connection = signal:Connect(function(result)
    if resultIsValid(result) then
        result = result
        connection:Disconnect()
    end
end)

while result == nil do
    task.wait()
end
return result

It would be great if Studio would give me a warning that setting a variable equal to itself doesn’t make sense and it’s probably a mistake.

8 Likes

Best fix is to avoid global vars and as soon as you see “a = a” then you should instantly realize there’s a problem. I prefer them spending hours on an useful feature rather than an intellisense check for bad code

3 Likes

You have two result variables as the connect variable and you initialized the variable as the same name outside the script. You aren’t changing the variable outside the script. Name the variables different names for it to work.

3 Likes

Ideally you’d get a warning that the function param is shadowing another variable.

7 Likes

This may be ideal behavior for some scripts though, albeit it’s slightly messy. Also, setting a local variable to itself may also be ideal if you want to create a clone of it with the same name:

local a = 1

do
    local a = a
    a = a + 1
    print(a) --> 2
end

print(a) --> 1

I agree that just a = a alone should give a warning. I have accidently done this before when mass renaming variables, and something happens to be that name somewhere deep in the code!

2 Likes

Why not just, y’know, have good coding practices and proofread your work? This is something I can’t see having much use

1 Like

The entire point of script analysis is to highlight problem areas in your code. Wouldn’t you agree that it’s good that the script editor highlights things like using a nonexistent variable, even though that’s also a fairly obvious mistake? It’s easy to identify mistakes in a small code sample like the deliberately simple one I included above, but when you’re debugging a silent bug across a gigantic project, incorrect code just blends in as more code.

5 Likes

CC @Boeufplume6771
This is a basic function in other IDEs such as Visual Studio, Rider, etc. The fact of the matter is that after writing thousands of lines of code, you are going to make an oversight mistake somewhere. This is a very valuable quality of life linting feature.

Let’s try to leave more useful replies to feature requests, rather than pretending we are better than other people, or that we understand engineering resource alloc and priorities. :slight_smile:

5 Likes

Shadowing is actually feature of Luau but unfortunately not all of the linting are enabled in Roblox Studio - hoping someday all of these will be enabled.

Aka… “Why don’t you try being perfect and make no mistakes ever?” Try doing that yourself. It’s impossible, but making mistakes is normal. The feature request would make finding this specific mistake much easier.

2 Likes

This seems to almost exist,

-- localshadow.luau

--!strict

result = 5

local function f(result)
    result = result
    
    return result
end

print(f(`World{result}`))

>luau-analyze localshadow.luau
localshadow.luau(5,1): TypeError: Unknown global 'result'
localshadow.luau(7,18): LocalShadow: Variable 'result' shadows a global variable used at line 5

, except prefixing result = 5 with local to get rid of the “Unknown global” warning removes the LocalShadow warning as well, and LocalShadow is disabled in Studio anyway:

And, there’s also the DuplicateLocal warning, but that only occurs in multi-assignments:

-- dupelocal.luau(1,15): DuplicateLocal: Variable 'result' already defined on column 7
local result, result = 5, 15
2 Likes

Yeah mb, I juste woke up and I was like “huh”. And my code structure is shit so :melting_face: