Skip to content

Remove false negatives with seq() in seq_linter() - #1468

Merged
IndrajeetPatil merged 21 commits into
mainfrom
1467_seq_linter
Jul 26, 2022
Merged

IndrajeetPatil merged 21 commits into
mainfrom
1467_seq_linter

Conversation

@IndrajeetPatil

@IndrajeetPatil IndrajeetPatil commented Jul 24, 2022 •

Copy link
Copy Markdown
Contributor

Closes #1467

TODO

  • Get rid of false negative
  • Tests should pass
  • Correct lint message should be produced
  • Update documentation (needs bumping roxygen2 version)
  • Address code review comments

Example

library(lintr)

lint(
  text = "seq(length(mtcars))",
  linters = seq_linter()
)
#> <text>:1:1: warning: [seq_linter] seq(length(...)) is likely to be wrong in the empty edge case. Use seq_along(...) instead.
#> seq(length(mtcars))
#> ^~~~~~~~~~~~~~~~~~~

lint(
  text = "seq(nrow(mtcars))",
  linters = seq_linter()
)
#> <text>:1:1: warning: [seq_linter] seq(nrow(...)) is likely to be wrong in the empty edge case. Use seq_len(...) instead.
#> seq(nrow(mtcars))
#> ^~~~~~~~~~~~~~~~~

Created on 2022-07-24 by the reprex package (v2.0.1)

@IndrajeetPatil IndrajeetPatil changed the title Remove false positives in seq_linter() Remove false negative in seq_linter() Jul 24, 2022
@IndrajeetPatil
IndrajeetPatil marked this pull request as ready for review July 24, 2022 07:58
Comment thread R/seq_linter.R Resolved
@MichaelChirico MichaelChirico added this to the 3.0.1 milestone Jul 25, 2022
@IndrajeetPatil

This comment was marked as resolved.

@MichaelChirico

This comment was marked as resolved.

Comment thread tests/testthat/test-seq_linter.R Resolved
Comment thread R/seq_linter.R Resolved
Comment thread R/seq_linter.R Resolved
@IndrajeetPatil

Copy link
Copy Markdown
Contributor Author

@MichaelChirico I think I've addressed all of your comments. So this should be all set.

})

test_that("finds seq(...) expressions", {
linter <- seq_linter()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could you add a test for seq(dim(x)[1]) which is how dim would usually be used? it's pretty rare (never seen it before myself), so if the current approach doesn't work, just file a follow-up issue.

but if the current xpath is good enough just add tests to prevent regression

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

if the current xpath is good enough just add tests to prevent regression

The current approach does work. Added a test.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, wait. This should actually produce a lint:

seq(dim(data.frame())[1])
#> [1] 1 0

Created on 2022-07-26 by the reprex package (v2.0.1)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

right... don't worry about it though. just please file a follow-up issue

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

and plz revert the incorrect test :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is it okay if I add the correct test but comment it out for now?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Created issue to track this: #1474

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

yep that's fine, though we'll have to be wary of comment_linter

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

see :)

@IndrajeetPatil
IndrajeetPatil merged commit a99b3c1 into main Jul 26, 2022
@IndrajeetPatil
IndrajeetPatil deleted the 1467_seq_linter branch July 26, 2022 20:00
@IndrajeetPatil IndrajeetPatil changed the title Remove false negative in seq_linter() Remove false negatives with seq() in seq_linter() Jul 26, 2022
@MichaelChirico MichaelChirico modified the milestones: 3.0.3, 3.1.0 Mar 20, 2023
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.

seq_linter should catch seq(length()), redirecting to seq_along()

2 participants