Skip to content
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

Eliminate unnecessary uses of rethrow(e) #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/linting/extended_checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ struct UseOfStaticThreads <: ViolationLintRule end
struct LogStatementsMustBeSafe <: FatalLintRule end
struct AssertionStatementsMustBeSafe <: ViolationLintRule end
struct NonFrontShapeAPIUsageRule <: FatalLintRule end
struct RethrowAndExceptionRule <: ViolationLintRule end

const all_extended_rule_types = Ref{Vector{DataType}}(
vcat(
Expand Down Expand Up @@ -659,3 +660,16 @@ function check(t::AssertionStatementsMustBeSafe, x::EXPR, markers::Dict{Symbol,S
end
end

function check(t::RethrowAndExceptionRule, x::EXPR)
generic_check(
t,
x,
"try
hole_variable_star
catch hole_variableA
hole_variable_star
rethrow(hole_variableA)
hole_variable_star
end",
"Change `rethrow(e)` into `rethrow()`.")
end
53 changes: 53 additions & 0 deletions test/exceptions_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
@testset "Exception-related rules" begin
@testset "Eliminate unnecessary rethrow(e)" begin
source = raw"""
function f()
try
print("Hello")
catch e
print("World")
# error handling code / error logging
rethrow(e)
end
end
"""
@test count_lint_errors(source) == 1
@test lint_test(source, "Line 2, column 5: Change `rethrow(e)` into `rethrow()`.")
end

@testset "Acceptable rethrow 1" begin
source = raw"""
function f()
try
print("Hello")
catch e
print("World")
e2 = Exception("Error")
# error handling code / error logging
rethrow(e2)
end
end
"""
# @test count_lint_errors(source) == 0
@test lint_test(source, "Lined 2, column 5: Change `rethrow(e)` into `rethrow()`.")

end

@testset "Acceptable rethrow 2" begin
source = raw"""
function f()
try
print("Hello")
catch e
print("World")
e2 = Exception("Error")
# error handling code / error logging
rethrow()
end
end
"""
# @test count_lint_errors(source) == 0
@test lint_test(source, "Lined 2, column 5: Change `rethrow(e)` into `rethrow()`.")

end
end
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ using ReLint: convert_offset_to_line_from_lines, check_all

include(joinpath(@__DIR__, "common.jl"))


include(joinpath(@__DIR__, "exceptions_tests.jl"))
include(joinpath(@__DIR__, "safe_logging_tests.jl"))
include(joinpath(@__DIR__, "lint_context_tests.jl"))
include(joinpath(@__DIR__, "rai_rules_tests.jl"))
Expand Down
Loading