Rubocop Style/GuardClause and Style/MultilineIfModifier conflict with each other
I sometimes have Style/GuardClause
and Style/MultilineIfModifier
arguing with each other over whether something should be a guard clause or not, and I end up having to disable one of them.
\
Here’s one example:
# rubocop:disable Style/GuardClause
if response.error?
errors = response.payload[:errors]
Rails.logger.error(
"DEBUGGING: Error calling Workspaces UpdateService from WorkspaceDevfileSyncWorker: #{errors}"
)
end
# rubocop:enable Style/GuardClause
versus:
# rubocop:disable Style/MultilineIfModifier
Rails.logger.error(
'DEBUGGING: Error calling Workspaces UpdateService from WorkspaceDevfileSyncWorker: ' \
"#{response.payload[:errors]}"
) if response.error?
# rubocop:enable Style/MultilineIfModifier
The issue is that it's a long message that puts you over the max line length.
So, if you write it as a end-of-line conditional like the second case, using \
to split the string across lines, then the Style/MultilineIfModifier
cop complains.
But if you shorten the line into multiple expressions, e.g. by pulling the interpolated part out to a separate variable as I did in the first case, then you have to wrap it in a block-style (not end-of-line) conditional, and then the Style/GuardClause
cop complains.
Edited by Chad Woolley