Skip to content

Lint nrow, ncol, NROW and NCOL with logical expressions - #2952

Merged
MichaelChirico merged 4 commits into
r-lib:mainfrom
mcol:issue_2933
Oct 17, 2025
Merged

MichaelChirico merged 4 commits into
r-lib:mainfrom
mcol:issue_2933

Conversation

@mcol

@mcol mcol commented Oct 17, 2025

Copy link
Copy Markdown
Contributor

I've taken a stab to fix #2933. I'm still unsure about the wording in the help page, at the moment I've only applied minimal changes. Also in terms of tests, I've used patrick in one case, but manually added some other no_lint tests, as I was not sure that it was worth the effort. I'm open to suggestions!

Comment thread R/length_test_linter.R Outdated
@@ -1,4 +1,4 @@
#' Check for a common mistake where length is applied in the wrong place
#' Check for a common mistake where length/nrow/ncol/NROW/NCOL is applied in the wrong place

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.

Suggested change
#' Check for a common mistake where length/nrow/ncol/NROW/NCOL is applied in the wrong place
#' Check for a common mistake where a size check like 'length' is applied in the wrong place

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 mention nrow/ncol/NROW/NCOL in the description below, as well as adding one new example for any of those.

Comment thread R/length_test_linter.R Outdated
lint_message <- sprintf(
"Checking the length of a logical vector is likely a mistake. Did you mean `length(%s) %s %s`?",
expr_parts[1L, ], expr_parts[2L, ], expr_parts[3L, ]
"Checking the length of a logical vector is likely a mistake. Did you mean `%s(%s) %s %s`?",

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.

Maybe it should be "Checking the %s of a logical vector" too?

@codecov

codecov Bot commented Oct 17, 2025 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.24%. Comparing base (aff43f1) to head (d7774b4).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2952   +/-   ##
=======================================
  Coverage   99.24%   99.24%           
=======================================
  Files         129      129           
  Lines        7276     7277    +1     
=======================================
+ Hits         7221     7222    +1     
  Misses         55       55           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

)
},
fun = funs,
.test_name = names(fun)

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.

I think we can drop .test_name here and just use fun = c("length", ...)

ops <- c(lt = "<", lte = "<=", gt = ">", gte = ">=", eq = "==", neq = "!=")
funs <- rep(c(length = "length", nrow = "nrow", ncol = "ncol", NROW = "NROW", NCOL = "NCOL"),
each = length(ops))
ops <- rep(ops, length(unique(funs)))

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.

this looks like expand.grid(), right? You can use .cases= in with_parameters_test_that() here, c.f.

cases <- expand.grid(
list_mapper = c("lapply", "map", "Map", ".mapply"),
comparator = c("==", "!=", ">=", "<=", ">", "<")
)
cases$.test_name <- with(cases, paste(list_mapper, comparator))
patrick::with_parameters_test_that(
"list_comparison_linter blocks simple disallowed usages",
expect_lint(sprintf("%s(x, sum) %s 10", list_mapper, comparator), lint_msg, linter),
.cases = cases
)

@MichaelChirico MichaelChirico left a comment

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.

Basically LGTM, just requesting some touch-up.

@MichaelChirico
MichaelChirico merged commit 27a959d into r-lib:main Oct 17, 2025
@mcol
mcol deleted the issue_2933 branch October 17, 2025 22:30
TimTaylor pushed a commit to TimTaylor/lintr that referenced this pull request Oct 29, 2025
* Lint nrow, ncol, NROW and NCOL with logical expressions.

* Address review comments.

* Fix indentation.

* tidy up tests

---------

Co-authored-by: Michael Chirico <[email protected]>
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.

Include nrow() and ncol() in length_test_linter()

2 participants