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

Adding unlock #16

Merged
merged 1 commit into from
Feb 27, 2024
Merged
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
20 changes: 15 additions & 5 deletions src/linting/extended_checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct Semaphore_Extension <: ExtendedRule end
struct Destructor_Extension <: ExtendedRule end
struct ReentrantLock_Extension <: ExtendedRule end
struct SpinLock_Extension <: ExtendedRule end
struct Lock_Extension <: ExtendedRule end
struct Unlock_Extension <: ExtendedRule end

const all_extended_rule_types = InteractiveUtils.subtypes(ExtendedRule)

Expand Down Expand Up @@ -96,10 +98,9 @@ function check(::NThreads_Extention, x::EXPR, markers::Dict{Symbol,Symbol})
generic_check(x, "Threads.nthreads()", "Threads.nthreads() should not be used in a constant variable.")
end

check(::CFunction_Extension, x::EXPR) = generic_check(x, "@cfunction(hole_variable, hole_variable_star)", "Macro @cfunction should not be used.")

check(::Semaphore_Extension, x::EXPR) = generic_check(x, "Semaphore(hole_variable)", "Semaphore should be used with extreme caution.")
check(::ReentrantLock_Extension, x::EXPR) = generic_check(x, "ReentrantLock()", "ReentrantLock should be used with extreme caution.")
check(::CFunction_Extension, x::EXPR) = generic_check(x, "@cfunction(hole_variable, hole_variable_star)", "Macro `@cfunction` should not be used.")
check(::Semaphore_Extension, x::EXPR) = generic_check(x, "Semaphore(hole_variable)", "`Semaphore` should be used with extreme caution.")
check(::ReentrantLock_Extension, x::EXPR) = generic_check(x, "ReentrantLock()", "`ReentrantLock` should be used with extreme caution.")

function check(::Destructor_Extension, x::EXPR)
error_msg = "Destructors should be used with extreme caution."
Expand All @@ -112,4 +113,13 @@ function check(::SpinLock_Extension, x::EXPR)
generic_check(x, "SpinLock()", msg)
generic_check(x, "Threads.SpinLock()", msg)
generic_check(x, "Base.Threads.SpinLock()", msg)
end
end

function check(::Lock_Extension, x::EXPR)
msg = "`@lock` should be used with extreme caution."
generic_check(x, "@lock hole_variable hole_variable", msg)
generic_check(x, "Base.@lock hole_variable hole_variable", msg)
end

check(::Unlock_Extension, x::EXPR) = generic_check(x, "unlock(hole_variable)", "`unlock` should be used with extreme caution.")

28 changes: 28 additions & 0 deletions test/rai_rules_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,32 @@ end
@test lint_test(source,
"Line 6, column 19: SpinLock should be used with extreme caution.")
end

@testset "unlock" begin
source = """
function clear(fs::SimulatedFs)
if fs.noop_mode
return nothing
end
for partition in fs.partitions
lock = trylock(partition.lock)
lock || error("SimFs partition locked on clear")
for (k, entry) in partition.entries
lock = trylock(entry.lock)
lock || error("SimFs entry locked on clear")
Blobs.free(entry.buf.data)
unlock(entry.lock)
end
empty!(partition.entries)
unlock(partition.lock)
end
@atomic fs.used_bytes = 0
end
"""
@test lint_has_error_test(source)
@test lint_test(source,
"Line 15, column 9: `unlock` should be used with extreme caution.")
end
end

@testset "Comparison" begin
Expand Down Expand Up @@ -372,6 +398,8 @@ end
end
"""
@test t(source, "finalizer(hole_variable) do hole_variable hole_variable_star end")

@test t("unlock(hole_variable)", "unlock(12)")
end

@testset "offset to line" begin
Expand Down
Loading