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

Add new Rails/UnusedRenderContent cop #970

Merged
merged 1 commit into from
Sep 7, 2023

Conversation

samrjenkins
Copy link
Contributor

@samrjenkins samrjenkins commented Apr 2, 2023

Issue: #967

The Rails documentation states:

If you try to render content along with a non-content status code (100-199, 204, 205, or 304), it will be dropped from the response.

https://guides.rubyonrails.org/layouts_and_rendering.html

On a number of occasions I have seen controller actions containing code such as:

render status: 204, plain: 'foo'

In cases such as these, the specified content for the response body is ignored and the code should be refactored to:

render status: 204

This pull request introduces a cop which identifies instances of unused body content.


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

@samrjenkins samrjenkins force-pushed the add-unused-render-content-cop branch 6 times, most recently from 86fe4df to fac79f3 Compare April 7, 2023 16:43
@samrjenkins samrjenkins marked this pull request as ready for review April 8, 2023 08:32
@@ -1096,6 +1096,11 @@ Rails/UnusedIgnoredColumns:
Include:
- app/models/**/*.rb

Rails/UnusedRenderContent:
Description: 'Do not specify body content for a response with a non-content status code.'
Enabled: pending
Copy link
Member

@koic koic Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add Severity: warning because this looks like a Lint-equivalent rule.

+ Severity: warning

Comment on lines 9 to 11
# This cop checks for uses of `render` which specify both body content and a non-content status.

# @example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# required.

Suggested change
# This cop checks for uses of `render` which specify both body content and a non-content status.
# @example
# This cop checks for uses of `render` which specify both body content and a non-content status.
#
# @example

@samrjenkins samrjenkins force-pushed the add-unused-render-content-cop branch from 2356123 to 25901bc Compare September 6, 2023 17:21
@samrjenkins
Copy link
Contributor Author

Thanks for your feedback, @koic. I have addressed both comments. I also rebased onto latest master to address issues of failing tests. Let me know if you would like me to squash the new commit.

@koic
Copy link
Member

koic commented Sep 7, 2023

@samrjenkins Yeah, looks good to me. Can you squash these commits into one?

@samrjenkins samrjenkins force-pushed the add-unused-render-content-cop branch from 25901bc to 8555ac5 Compare September 7, 2023 16:47
@samrjenkins
Copy link
Contributor Author

@koic done 👍

@koic koic merged commit e465675 into rubocop:master Sep 7, 2023
@koic
Copy link
Member

koic commented Sep 7, 2023

Thanks!

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

Successfully merging this pull request may close these issues.

2 participants