Skip to content

Performance/DeleteSuffix is not safe #245

@jmkoni

Description

@jmkoni

We had a user of StandardRB open up standardrb/standard#292.

Expected behavior

Currently, if you are using sub to remove a suffix on a Pathname object, it replaces it with delete_suffix, which is not a valid method on a Pathname object. I would expect the following not to change:

path = Pathname.new("path/to/some/ruby/file.rb").sub(/\.rb\z/, "")

This should either not change or the cop should be marked as not safe.

Actual behavior

It changes the above code to:

path = Pathname.new("path/to/some/ruby/file.rb").delete_suffix(".rb")

which results in the following error:

Traceback (most recent call last):
standardrb_bug.rb:3:in `<main>': undefined method `delete_suffix' for #<Pathname:path/to/some/ruby/file.rb> (NoMethodError)

Steps to reproduce the problem

Put the above code in a ruby file, fix the file with Performance/DeleteSuffix enabled. Then try to run the file.

RuboCop version

rubocop 1.14.0
rubocop-performance 1.11.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions