-
Notifications
You must be signed in to change notification settings - Fork 188
Unreachable loops #2141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unreachable loops #2141
Changes from 19 commits
5872b25
aeca511
82a22ba
db3869f
7483afb
a816863
70f05c5
7e408d5
3c490c3
2d482e6
c577c50
838d9f9
ea3820b
c22d7f0
2d1a316
f825ea9
134dcaa
cbd1352
ad448da
9cf4f24
68d349e
792904c
ce0cfa9
4640abb
23d3dc7
78952cf
f645ac3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,196 @@ test_that("unreachable_code_linter works in simple function", { | |
expect_lint(lines, NULL, unreachable_code_linter()) | ||
}) | ||
|
||
test_that("unreachable_code_linter works in sub expressions", { | ||
linter <- unreachable_code_linter() | ||
msg <- rex::rex("Code and comments coming after a return() or stop()") | ||
|
||
lines <- trim_some(" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. long tests like this are good for robustness, but best to start with very succinct tests of only one aspect of the lint logic. it means more lines of code which can be tedious, but is much preferable for test readability & ease of maintenance There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it may also help to either annotate the test with a comment on what all is being tested (it looks like "various types of if/else cases"), or to split into different test_that() and record that in the test's name |
||
foo <- function(bar) { | ||
if (bar) { | ||
return(bar) | ||
# Test comment | ||
while (bar) { | ||
return(bar) | ||
5 + 3 | ||
repeat { | ||
return(bar) | ||
# Test comment | ||
} | ||
} | ||
} else if (bla) { | ||
# test | ||
return(5) | ||
# Test 2 | ||
} else { | ||
return(bar) | ||
# Test comment | ||
for(i in 1:3) { | ||
return(bar) | ||
5 + 4 | ||
} | ||
} | ||
return(bar) | ||
5 + 1 | ||
} | ||
") | ||
|
||
expect_lint( | ||
lines, | ||
list( | ||
list(line_number = 4L, message = msg), | ||
list(line_number = 7L, message = msg), | ||
list(line_number = 10L, message = msg), | ||
list(line_number = 16L, message = msg), | ||
list(line_number = 19L, message = msg), | ||
list(line_number = 22L, message = msg), | ||
list(line_number = 26L, message = msg) | ||
), | ||
linter | ||
) | ||
|
||
lines <- trim_some(" | ||
foo <- function(bar) { | ||
if (bar) { | ||
return(bar) # Test comment | ||
} | ||
while (bar) { | ||
return(bar) # 5 + 3 | ||
} | ||
repeat { | ||
return(bar) # Test comment | ||
} | ||
|
||
} | ||
") | ||
|
||
expect_lint(lines, NULL, linter) | ||
|
||
lines <- trim_some(" | ||
foo <- function(bar) { | ||
if (bar) { | ||
return(bar); x <- 2 | ||
} else { | ||
return(bar); x <- 3 | ||
} | ||
while (bar) { | ||
return(bar); 5 + 3 | ||
} | ||
repeat { | ||
return(bar); test() | ||
} | ||
for(i in 1:3) { | ||
return(bar); 5 + 4 | ||
} | ||
} | ||
") | ||
|
||
expect_lint( | ||
lines, | ||
list( | ||
list(line_number = 3L, message = msg), | ||
list(line_number = 5L, message = msg), | ||
list(line_number = 8L, message = msg), | ||
list(line_number = 11L, message = msg), | ||
list(line_number = 14L, message = msg) | ||
), | ||
linter | ||
) | ||
}) | ||
|
||
test_that("unreachable_code_linter works with next and break in sub expressions", { | ||
linter <- unreachable_code_linter() | ||
msg <- rex::rex("Code and comments coming after a `next` or `break`") | ||
|
||
lines <- trim_some(" | ||
foo <- function(bar) { | ||
if (bar) { | ||
next | ||
MEO265 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Test comment | ||
while (bar) { | ||
break | ||
5 + 3 | ||
repeat { | ||
next | ||
# Test comment | ||
} | ||
} | ||
} else { | ||
next | ||
# test | ||
for(i in 1:3) { | ||
break | ||
5 + 4 | ||
} | ||
} | ||
} | ||
") | ||
|
||
expect_lint( | ||
lines, | ||
list( | ||
list(line_number = 4L, message = msg), | ||
list(line_number = 7L, message = msg), | ||
list(line_number = 10L, message = msg), | ||
list(line_number = 15L, message = msg), | ||
list(line_number = 18L, message = msg) | ||
), | ||
linter | ||
) | ||
|
||
lines <- trim_some(" | ||
foo <- function(bar) { | ||
if (bar) { | ||
break # Test comment | ||
} else { | ||
next # Test comment | ||
} | ||
while (bar) { | ||
next # 5 + 3 | ||
} | ||
repeat { | ||
next # Test comment | ||
} | ||
for(i in 1:3) { | ||
break # 5 + 4 | ||
} | ||
} | ||
") | ||
|
||
expect_lint(lines, NULL, linter) | ||
|
||
lines <- trim_some(" | ||
foo <- function(bar) { | ||
if (bar) { | ||
next; x <- 2 | ||
} else { | ||
break; x <- 3 | ||
} | ||
while (bar) { | ||
break; 5 + 3 | ||
} | ||
repeat { | ||
next; test() | ||
} | ||
for(i in 1:3) { | ||
break; 5 + 4 | ||
} | ||
} | ||
") | ||
|
||
expect_lint( | ||
lines, | ||
list( | ||
list(line_number = 3L, message = msg), | ||
list(line_number = 5L, message = msg), | ||
list(line_number = 8L, message = msg), | ||
list(line_number = 11L, message = msg), | ||
list(line_number = 14L, message = msg) | ||
), | ||
linter | ||
) | ||
}) | ||
|
||
test_that("unreachable_code_linter ignores expressions that aren't functions", { | ||
expect_lint("x + 1", NULL, unreachable_code_linter()) | ||
}) | ||
|
@@ -57,7 +247,7 @@ test_that("unreachable_code_linter identifies simple unreachable code", { | |
lines, | ||
list( | ||
line_number = 3L, | ||
message = rex::rex("Code and comments coming after a top-level return() or stop()") | ||
message = rex::rex("Code and comments coming after a return() or stop()") | ||
), | ||
unreachable_code_linter() | ||
) | ||
|
@@ -73,13 +263,13 @@ test_that("unreachable_code_linter finds unreachable comments", { | |
") | ||
expect_lint( | ||
lines, | ||
rex::rex("Code and comments coming after a top-level return() or stop()"), | ||
rex::rex("Code and comments coming after a return() or stop()"), | ||
unreachable_code_linter() | ||
) | ||
}) | ||
|
||
test_that("unreachable_code_linter finds expressions in the same line", { | ||
msg <- rex::rex("Code and comments coming after a top-level return() or stop()") | ||
msg <- rex::rex("Code and comments coming after a return() or stop()") | ||
linter <- unreachable_code_linter() | ||
|
||
lines <- trim_some(" | ||
|
@@ -107,7 +297,7 @@ test_that("unreachable_code_linter finds expressions in the same line", { | |
}) | ||
|
||
test_that("unreachable_code_linter finds expressions and comments after comment in return line", { | ||
msg <- rex::rex("Code and comments coming after a top-level return() or stop()") | ||
msg <- rex::rex("Code and comments coming after a return() or stop()") | ||
linter <- unreachable_code_linter() | ||
|
||
lines <- trim_some(" | ||
|
@@ -136,7 +326,7 @@ test_that("unreachable_code_linter finds a double return", { | |
") | ||
expect_lint( | ||
lines, | ||
rex::rex("Code and comments coming after a top-level return() or stop()"), | ||
rex::rex("Code and comments coming after a return() or stop()"), | ||
unreachable_code_linter() | ||
) | ||
}) | ||
|
@@ -151,7 +341,7 @@ test_that("unreachable_code_linter finds code after stop()", { | |
") | ||
expect_lint( | ||
lines, | ||
rex::rex("Code and comments coming after a top-level return() or stop()"), | ||
rex::rex("Code and comments coming after a return() or stop()"), | ||
unreachable_code_linter() | ||
) | ||
}) | ||
|
@@ -195,6 +385,20 @@ test_that("unreachable_code_linter ignores terminal nolint end comments", { | |
NULL, | ||
list(unreachable_code_linter(), one_linter = assignment_linter()) | ||
) | ||
|
||
expect_lint( | ||
trim_some(" | ||
foo <- function() { | ||
do_something | ||
# nolint start: one_linter. | ||
a = 42 | ||
next | ||
# nolint end | ||
} | ||
"), | ||
NULL, | ||
unreachable_code_linter() | ||
) | ||
}) | ||
|
||
test_that("unreachable_code_linter identifies unreachable code in conditional loops", { | ||
|
@@ -349,7 +553,7 @@ test_that("unreachable_code_linter identifies unreachable code in mixed conditio | |
), | ||
list( | ||
line_number = 13L, | ||
message = rex::rex("Code and comments coming after a top-level return() or stop()") | ||
message = rex::rex("Code and comments coming after a return() or stop()") | ||
) | ||
), | ||
linter | ||
|
@@ -389,7 +593,7 @@ test_that("unreachable_code_linter identifies unreachable code in mixed conditio | |
# ") | ||
# expect_lint( | ||
# unreachable_inside_switch_lines, | ||
# rex::rex("Code and comments coming after a top-level return() or stop()"), | ||
# rex::rex("Code and comments coming after a return() or stop()"), | ||
# unreachable_code_linter() | ||
# ) | ||
# }) | ||
|
Uh oh!
There was an error while loading. Please reload this page.