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

Lint/UselessAssignment: bad behavior with pattern matching #9762

Closed
akimd opened this issue May 3, 2021 · 0 comments · Fixed by #9764
Closed

Lint/UselessAssignment: bad behavior with pattern matching #9762

akimd opened this issue May 3, 2021 · 0 comments · Fixed by #9764
Assignees
Labels

Comments

@akimd
Copy link

akimd commented May 3, 2021

Expected behavior

In the following example, res is obviously needed:

def f
  case '0'
  in String
    res = 1
  else
    res = 2
  end
  res
end

puts f

so Lint/UselessAssignment should not claim it is useless.

Actual behavior

Offenses:

patmatch.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
def f
^
patmatch.rb:4:5: W: Lint/UselessAssignment: Useless assignment to variable - res.
    res = 1
    ^^^

1 file inspected, 2 offenses detected, 1 offense auto-correctable

Please use rubocop --debug when pasting rubocop output as it contains additional information.

For /private/tmp/foo: configuration from /private/tmp/foo/.rubocop.yml
Inheriting configuration from /private/tmp/foo/.rubocop_todo.yml
Default configuration from /opt/local/lib/ruby3.0/gems/3.0.0/gems/rubocop-1.13.0/config/default.yml
The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.

Please also note that can also opt-in to new cops by default by adding this to your config:
  AllCops:
    NewCops: enable

Gemspec/DateAssignment: # (new in 1.10)
  Enabled: true
Layout/SpaceBeforeBrackets: # (new in 1.7)
  Enabled: true
Lint/AmbiguousAssignment: # (new in 1.7)
  Enabled: true
Lint/DeprecatedConstants: # (new in 1.8)
  Enabled: true
Lint/DuplicateBranch: # (new in 1.3)
  Enabled: true
Lint/DuplicateRegexpCharacterClassElement: # (new in 1.1)
  Enabled: true
Lint/EmptyBlock: # (new in 1.1)
  Enabled: true
Lint/EmptyClass: # (new in 1.3)
  Enabled: true
Lint/LambdaWithoutLiteralBlock: # (new in 1.8)
  Enabled: true
Lint/NoReturnInBeginEndBlocks: # (new in 1.2)
  Enabled: true
Lint/NumberedParameterAssignment: # (new in 1.9)
  Enabled: true
Lint/OrAssignmentToConstant: # (new in 1.9)
  Enabled: true
Lint/RedundantDirGlobSort: # (new in 1.8)
  Enabled: true
Lint/SymbolConversion: # (new in 1.9)
  Enabled: true
Lint/ToEnumArguments: # (new in 1.1)
  Enabled: true
Lint/TripleQuotes: # (new in 1.9)
  Enabled: true
Lint/UnexpectedBlockArity: # (new in 1.5)
  Enabled: true
Lint/UnmodifiedReduceAccumulator: # (new in 1.1)
  Enabled: true
Style/ArgumentsForwarding: # (new in 1.1)
  Enabled: true
Style/CollectionCompact: # (new in 1.2)
  Enabled: true
Style/DocumentDynamicEvalDefinition: # (new in 1.1)
  Enabled: true
Style/EndlessMethod: # (new in 1.8)
  Enabled: true
Style/HashConversion: # (new in 1.10)
  Enabled: true
Style/HashExcept: # (new in 1.7)
  Enabled: true
Style/IfWithBooleanLiteralBranches: # (new in 1.9)
  Enabled: true
Style/NegatedIfElseCondition: # (new in 1.2)
  Enabled: true
Style/NilLambda: # (new in 1.3)
  Enabled: true
Style/RedundantArgument: # (new in 1.4)
  Enabled: true
Style/StringChars: # (new in 1.12)
  Enabled: true
Style/SwapValues: # (new in 1.1)
  Enabled: true
For more information: https://docs.rubocop.org/rubocop/versioning.html
Inspecting 1 file
Scanning /private/tmp/foo/patmatch.rb
W

Offenses:

patmatch.rb:1:1: C: [Correctable] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
def f
^
patmatch.rb:4:5: W: Lint/UselessAssignment: Useless assignment to variable - res.
    res = 1
    ^^^

1 file inspected, 2 offenses detected, 1 offense auto-correctable
Finished in 0.1654020000132732 seconds

Steps to reproduce the problem

See above.

RuboCop version

$ rubocop -V
1.13.0 (using Parser 3.0.1.0, rubocop-ast 1.4.1, running on ruby 3.0.1 x86_64-darwin18)

Greetings

Thanks for rubocop!

@dvandersluis dvandersluis self-assigned this May 3, 2021
dvandersluis added a commit to dvandersluis/rubocop that referenced this issue May 3, 2021
@koic koic closed this as completed in #9764 May 3, 2021
koic added a commit that referenced this issue May 3, 2021
[Fix #9762] Update `VariableForce` to be able to handle `case-match` nodes
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.

2 participants