Skip to content

Correct axis for scoped lint in implicit_assignment_linter - #2914

Merged
MichaelChirico merged 3 commits into
mainfrom
implicit-scoped-exprlist
Aug 4, 2025
Merged

MichaelChirico merged 3 commits into
mainfrom
implicit-scoped-exprlist

Conversation

@MichaelChirico

@MichaelChirico MichaelChirico commented Jul 31, 2025 •

Copy link
Copy Markdown
Collaborator

Closes #2913

This previously assumed a nested implicit assignment like if (foo(l <- r)), the correction allows for plain assignments as observed if (l <- r).

@codecov

codecov Bot commented Jul 31, 2025 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2914   +/-   ##
=======================================
  Coverage   99.28%   99.28%           
=======================================
  Files         129      129           
  Lines        7266     7266           
=======================================
  Hits         7214     7214           
  Misses         52       52           

☔ 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.

@Bisaloo Bisaloo 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.

Isn't this supposed to lint?

lintr::lint(
  text = "if (a <- 1) print(b)", 
  linters = lintr::implicit_assignment_linter(allow_scoped = TRUE)
)
#> ℹ No lints found.

Created on 2025-07-31 with reprex v2.1.1

Session info

sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R Under development (unstable) (2025-07-15 r88411)
#>  os       Ubuntu 24.04.2 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language en_US
#>  collate  en_GB.UTF-8
#>  ctype    en_GB.UTF-8
#>  tz       Europe/Paris
#>  date     2025-07-31
#>  pandoc   3.6.3 @ /usr/share/positron/resources/app/quarto/bin/tools/x86_64/ (via rmarkdown)
#>  quarto   1.8.14 @ /opt/quarto/bin/quarto
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date (UTC) lib source
#>  backports      1.5.0      2024-05-23 [1] RSPM (R 4.6.0)
#>  cli            3.6.5      2025-04-23 [1] RSPM (R 4.6.0)
#>  digest         0.6.37     2024-08-19 [1] RSPM
#>  evaluate       1.0.4      2025-06-18 [1] RSPM (R 4.6.0)
#>  fastmap        1.2.0      2024-05-15 [1] RSPM (R 4.6.0)
#>  fs             1.6.6      2025-04-12 [1] RSPM (R 4.6.0)
#>  glue           1.8.0      2024-09-30 [1] RSPM (R 4.6.0)
#>  htmltools      0.5.8.1    2024-04-04 [1] RSPM (R 4.6.0)
#>  knitr          1.50       2025-03-16 [1] RSPM (R 4.6.0)
#>  lazyeval       0.2.2      2019-03-15 [1] RSPM (R 4.6.0)
#>  lifecycle      1.0.4.9000 2025-07-19 [1] Github (r-lib/lifecycle@2a852fe)
#>  lintr          3.2.0.9000 2025-07-31 [1] Github (r-lib/lintr@384012b)
#>  reprex         2.1.1      2024-07-06 [1] RSPM
#>  rex            1.2.1      2021-11-26 [1] RSPM (R 4.6.0)
#>  rlang          1.1.6      2025-04-11 [1] RSPM (R 4.6.0)
#>  rmarkdown      2.29       2024-11-04 [1] RSPM
#>  rstudioapi     0.17.1     2024-10-22 [1] RSPM (R 4.6.0)
#>  sessioninfo    1.2.3      2025-02-05 [1] RSPM (R 4.6.0)
#>  withr          3.0.2      2024-10-28 [1] RSPM (R 4.6.0)
#>  xfun           0.52       2025-04-02 [1] RSPM (R 4.6.0)
#>  xml2           1.3.8      2025-03-14 [1] RSPM (R 4.6.0)
#>  xmlparsedata   1.0.5      2021-03-06 [1] RSPM (R 4.6.0)
#>  yaml           2.3.10     2024-07-26 [1] RSPM (R 4.6.0)
#> 
#>  [1] /home/hugo/.local/share/R/x86_64-pc-linux-gnu-library/4.6
#>  [2] /opt/R/devel/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

@MichaelChirico

MichaelChirico commented Jul 31, 2025 •

Copy link
Copy Markdown
Collaborator Author

I think I misspoke in the issue 🤭

the current logic ignores the actual branch entirely

I think object_usage_linter can deal with that in more realistic examples, especially after it is working on scripts.

I would handle that as a follow-up in any case

@Bisaloo

Bisaloo commented Aug 3, 2025

Copy link
Copy Markdown
Collaborator

Oh okay, I get it now. a doesn't necessarily have to be used at all, as long as it's not also used outside of the branch. That's then indeed more a case for object_usage_linter().

@Bisaloo Bisaloo 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.

I think Tim's case was addressed so feel free to merge.

I think we have however another edge case to cover at some point:

lintr::lint(
  text = "if (a <- 1 && b <- 2) { print(a) }
  print(a)", 
  linters = lintr::implicit_assignment_linter(allow_scoped = TRUE)
)
#> <text>:1:5: warning: [implicit_assignment_linter] Avoid implicit assignments in function calls. For example, instead of `if (x <- 1L) { ... }`, write `x <- 1L; if (x) { ... }`.
#> if (a <- 1 && b <- 2) { print(a) }
#>     ^~~~~~~~~~~~~~~~

lintr::lint(
  text = "if (a <- 1 && b <- 2) { print(b) }
  print(b)", 
  linters = lintr::implicit_assignment_linter(allow_scoped = TRUE)
)
#> ℹ No lints found.

Created on 2025-08-03 with reprex v2.1.1

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Oh okay, I get it now. a doesn't necessarily have to be used at all, as long as it's not also used outside of the branch. That's then indeed more a case for object_usage_linter().

Yea, that's the current behavior. I'm not married to it... but it does seem a bit impractical (i.e., not likely to come up in real code). I can think of two cases:

  1. Refactoring. This should probably be linted as an alert to the coder (did you forget this?) But that fits pretty squarely in what object_usage_linter() is for.
  2. Active development. We wrote the if() statement but haven't finished the branch yet; in this case, the IDE might start showing a lint right away. Mildly annoying, but these are all over the place / you get used to it, I guess.

@MichaelChirico
MichaelChirico merged commit 84d3f8c into main Aug 4, 2025
@MichaelChirico
MichaelChirico deleted the implicit-scoped-exprlist branch August 4, 2025 16:18
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.

How do we exclude if from the implicit_assignment_linter?

2 participants