Skip to content

Make Performance/Sum aware of arrays #495

Description

@viralpraxis

Expected behavior

Currently, RuboCop suggest to correct expressions like

%w[a* b*].map { |e| Dir.glob(e) }.reduce(&:+)

to

%w[a* b*].map { |e| Dir.glob(e) }.sum

Since Dir.glob is known to return an array we can autocorrect to sum([]) instead. This can be generalized to other stdlib methods returning arrays

Actual behavior

Unsafe autocorrection is %w[a* b*].map { |e| Dir.glob(e) }.sum, which is invalid.

Steps to reproduce the problem

N/A

RuboCop version

main

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions