Skip to content

Unexpected [Lint/UselessAssign] Useless assignment to variable #342

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

Closed
zw963 opened this issue Jan 15, 2023 · 3 comments · Fixed by #351
Closed

Unexpected [Lint/UselessAssign] Useless assignment to variable #342

zw963 opened this issue Jan 15, 2023 · 3 comments · Fixed by #351
Assignees
Labels
Milestone

Comments

@zw963
Copy link
Contributor

zw963 commented Jan 15, 2023

Following is a code example:

# 1.cr

require "option_parser"

module Translater
  target_language : String? = nil

  OptionParser.parse do |parser|
    parser.banner = <<-USAGE
Usage: translater <option> content
USAGE

    parser.on(
      "-t TARGET",
      "--target=TARGET",
      "Specify target language, support zh-CN|en for now.
default is translate English to Chinese.
Youdao don't support this option.
"
    ) do |target|
      case target
      when "zh-CN"
        target_language = "Chinese"
      when "en"
        target_language = "English"
      else
        STDERR.puts "Supported options: -t zh-CN|en"
        exit
      end
    end
  end

  p target_language
end

When run with ameba 1.cr, get following unexpected message:

1.cr:21:9
[W] Lint/UselessAssign: Useless assignment to variable `target_language`
> target_language = "Chinese"
  ^-------------^

1.cr:23:9
[W] Lint/UselessAssign: Useless assignment to variable `target_language`
> target_language = "English"
  ^-------------^

But, this is wrong, if move all code out of module Translater, ameba is fine, but it is same for those two cases.

@veelenga veelenga added the bug label Jan 15, 2023
@veelenga veelenga self-assigned this Jan 15, 2023
@Sija
Copy link
Member

Sija commented Jan 15, 2023

That's expected since you're inside the module scope, so the variable defined is not really local.

@veelenga
Copy link
Member

veelenga commented Jan 16, 2023

While the code looks semantically bad, we still need to handle this edge case in Ameba (low priority)

@straight-shoota
Copy link
Contributor

That's expected since you're inside the module scope, so the variable defined is not really local.

target_language is actually a local variable defined in the top-level scope.

There are no type-level scopes for local variables.

module Translater
  target_language : String? = "foo"
end

target_language # => "foo"

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

Successfully merging a pull request may close this issue.

4 participants