Skip to content

Commit a4abe1d

Browse files
committed
Warn if @inheritParams used when not needed
Fixes #836
1 parent c15f265 commit a4abe1d

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# roxygen2 (development version)
22

3+
* `@inheritParams` warns if there are no parameters that require
4+
documentation (#836).
5+
36
* roxygen2 now recgonises fully qualified S4 functions like
47
`methods::setGeneric()`, `methods::setClass()` and `methods::setMethod()`
58
(#880).

R/rd-inherit.R

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,28 @@ topics_process_inherit <- function(topics, env) {
2626
# Inherit parameters -----------------------------------------------------------
2727

2828
inherit_params <- function(topic, topics) {
29+
inheritors <- topic$inherits_from("params")
30+
if (length(inheritors) == 0) {
31+
return()
32+
}
33+
2934
documented <- get_documented_params(topic)
3035
needed <- topic$get_field("formals")$values
31-
3236
missing <- setdiff(needed, documented)
3337
if (length(missing) == 0) {
38+
warn(paste0(
39+
"Topic '", topic$get_name(), "': ",
40+
"no parameters to inherit with @inheritParams"
41+
))
3442
return()
3543
}
3644

37-
for (inheritor in topic$inherits_from("params")) {
45+
for (inheritor in inheritors) {
3846
inherited <- find_params(inheritor, topics)
3947

4048
to_add <- intersect(missing, names(inherited))
4149
if (length(to_add) == 0) {
50+
# Can't warn here because @inherit inherits parameters
4251
next
4352
}
4453

R/topic.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ RoxyTopic <- R6::R6Class("RoxyTopic", public = list(
3232
self$fields[[field_name]]
3333
},
3434

35+
get_name = function() {
36+
self$get_field("name")$values
37+
},
38+
3539
inherits_from = function(type) {
3640
if (!self$has_field("inherit")) {
3741
return(character())

tests/testthat/test-rd-inherit.R

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,23 @@ test_that("@inheritParam understands compound docs", {
310310
#' Title
311311
#'
312312
#' @inheritParams x
313-
#' @param x y
314313
#' @param y y
315314
y <- function(x, y) {}")[[2]]
316315
params <- get_tag(out, "param")$values
317-
expect_equal(params, c(x = "y", y = "y"))
316+
expect_equal(params, c(x = "x", y = "y"))
318317
})
319318

319+
test_that("warned if no params need documentation", {
320+
code <- "
321+
#' Title
322+
#'
323+
#' @param x x
324+
#' @param y x
325+
#' @inheritParams foo
326+
x <- function(x, y) {}
327+
"
328+
expect_warning(roc_proc_text(rd_roclet(), code), "no parameters to inherit")
329+
})
320330

321331
test_that("argument order, also for incomplete documentation", {
322332
out <- roc_proc_text(rd_roclet(), "

0 commit comments

Comments
 (0)