Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give a warning if a local variable is assigned to but never read #76

Open
JukkaL opened this issue Jan 29, 2013 · 8 comments
Open

Give a warning if a local variable is assigned to but never read #76

JukkaL opened this issue Jan 29, 2013 · 8 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 29, 2013

The type checker should give a warning if a local variable is only assigned to but never read.
This is usually a result of a misspelled variable name.

Perhaps only do this if warnings have been explicitly enabled using a command line option or via some other mechanism.

@dubesar
Copy link

dubesar commented Feb 23, 2020

@JukkaL Do you mean something like this:

def foo(x:int) -> int:
    x = 5
    y = 5
    return x

,in this y maybe misspelled local variable which is assigned to but never read.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Feb 28, 2020

@dubesar Yeah! Or something like this:

def thing() -> str:
    processor = 'default'
    if cond():
        processsor = 'extra'  # Note misspelling -- extra 's'
    return processor

@sudhirkumar43
Copy link

Hi @JukkaL,
Its like point the error like processsor is not defined earlier? Since we don't know whether the two words are pointing the same variable or different.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 20, 2020

@sudhirkumar43 Can you rephrase your question? I wasn't quite sure what you mean exactly.

@sudhirkumar43
Copy link

sudhirkumar43 commented Mar 20, 2020

@JukkaL
From the above Example:-
def thing() -> str:
processor = 'default'
if cond():
processsor = 'extra' # Note misspelling -- extra 's'
return processor
here should it raise error like processsor is not defined earlier or we should print the error like the spelling error? Since we don't know whether the two words (processsor , processor ) are pointing the same variable or different. It may possible that processsor is the name of other variable. I think we can do one thing that just check if a word is similar to some predefined word then raise an error. for example, processsor is much similar to processor raise the error "either missplled word or variable is not defined earlier".

@JukkaL
Copy link
Collaborator Author

JukkaL commented Mar 20, 2020

It would be sufficient to give a message such as Value assigned to "processsor" is never read.

@sudhirkumar43
Copy link

@JukkaL
But here is a different case processsor is never initialized and then how can we use it?

@dosisod
Copy link
Contributor

dosisod commented Oct 8, 2022

@JukkaL, I have been dealing with a similar issue with Refurb (see comment), curious what your thoughts are on the following:

This currently passes in Mypy, and this issue proposes that a warning should be emitted:

x = 123  # WARN: value unused
x = 456

This, I believe is similar in nature, but gives a type error instead:

x = 123    # value is still unused here
x = "abc"  # but you get a type error here
main.py:2: error: Incompatible types in assignment (expression has type "str", variable has type "int")

Adding --allow-redefinition fixes this, but only if you read from x first:

x = 123
print(x)
x = "abc"

A few thoughts:

  • Do you think it is too strict of a requirement that x must be read first before a new variable is created? I feel like this behaviour gives you an inconvinent type error, when in reality it should be telling you that the value you used is never read (at least in this instance that is).

  • Wouldn't it be more advantageous that enabling allow_redefinition will make a new x variable whenever x is assigned, regardless of whether it has been read or not? If you kept the ref count, you would be able to tell pretty easily if the value had been read or not. This would bring us closer to how SSA works in say LLVM, as opposed to only creating a new variable when the type changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants