From bfd87ae3939e31e588c489e83b1d06875737b281 Mon Sep 17 00:00:00 2001 From: Alexandre Bergel Date: Tue, 18 Feb 2025 13:25:02 +0100 Subject: [PATCH] Eliminate unnecessary uses of `rethrow(e)` --- src/linting/extended_checks.jl | 14 +++++++++ test/exceptions_tests.jl | 53 ++++++++++++++++++++++++++++++++++ test/runtests.jl | 2 ++ 3 files changed, 69 insertions(+) create mode 100644 test/exceptions_tests.jl diff --git a/src/linting/extended_checks.jl b/src/linting/extended_checks.jl index 33f2fc3..92c7c1e 100644 --- a/src/linting/extended_checks.jl +++ b/src/linting/extended_checks.jl @@ -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( @@ -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 diff --git a/test/exceptions_tests.jl b/test/exceptions_tests.jl new file mode 100644 index 0000000..9c073a2 --- /dev/null +++ b/test/exceptions_tests.jl @@ -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 \ No newline at end of file diff --git a/test/runtests.jl b/test/runtests.jl index d3b66be..c6c0c3c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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"))