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

Literals are safe (except String) #105

Merged
merged 1 commit into from
Oct 30, 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
11 changes: 10 additions & 1 deletion src/linting/extended_checks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
!is_there_any_star_marker && return x == y

contains(x, "QQQ") && contains(y, "QQQ") &&
throw(BothCannotHaveStarException("Cannot both $x and $y have a star marker"))

Check failure on line 58 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

if contains(x, "QQQ")
reg_exp = Regex(replace(x, "QQQ" => ".*"))
return !isnothing(match(reg_exp, y))
Expand Down Expand Up @@ -290,7 +290,7 @@

function check(t::InitializingWithFunctionRule, x::EXPR, markers::Dict{Symbol,String})
# If we are not in a const statement, then we exit this function.
haskey(markers, :const) || return

Check failure on line 293 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `thaskey(dict,key)` instead of the Julia's `haskey`.
generic_check(t, x, "Threads.nthreads()", "`Threads.nthreads()` should not be used in a constant variable.")
generic_check(t, x, "is_local_deployment()", "`is_local_deployment()` should not be used in a constant variable.")
generic_check(t, x, "Deployment.is_local_deployment()", "`Deployment.is_local_deployment()` should not be used in a constant variable.")
Expand Down Expand Up @@ -346,11 +346,11 @@
check(t::PtrRule, x::EXPR) = generic_check(t, x, "Ptr{hole_variable}(hole_variable)")

function check(t::ArrayWithNoTypeRule, x::EXPR, markers::Dict{Symbol,String})
haskey(markers, :filename) || return

Check failure on line 349 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `thaskey(dict,key)` instead of the Julia's `haskey`.
contains(markers[:filename], "src/Compiler") || return

haskey(markers, :macrocall) && markers[:macrocall] == "@match" && return

Check failure on line 352 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `thaskey(dict,key)` instead of the Julia's `haskey`.
haskey(markers, :macrocall) && markers[:macrocall] == "@matchrule" && return

Check failure on line 353 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `thaskey(dict,key)` instead of the Julia's `haskey`.

generic_check(t, x, "[]", "Need a specific Array type to be provided.")
end
Expand Down Expand Up @@ -390,7 +390,7 @@
end

function check(t::UnsafeRule, x::EXPR, markers::Dict{Symbol,String})
haskey(markers, :function) || return

Check failure on line 393 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `thaskey(dict,key)` instead of the Julia's `haskey`.
isnothing(match(r"_unsafe_.*", markers[:function])) || return
isnothing(match(r"unsafe_.*", markers[:function])) || return

Expand Down Expand Up @@ -486,7 +486,7 @@
end

function check(t::RelPathAPIUsageRule, x::EXPR, markers::Dict{Symbol,String})
haskey(markers, :filename) || return

Check failure on line 489 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `thaskey(dict,key)` instead of the Julia's `haskey`.
contains(markers[:filename], "src/Compiler/Front") || return

generic_check(t, x, "hole_variable::RelPath", "Usage of type `RelPath` is not allowed in this context.")
Expand All @@ -498,7 +498,7 @@
end

function check(t::NonFrontShapeAPIUsageRule, x::EXPR, markers::Dict{Symbol,String})
haskey(markers, :filename) || return

Check failure on line 501 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `thaskey(dict,key)` instead of the Julia's `haskey`.
# In the front-end and in FFI, we are allowed to refer to `Shape`
contains(markers[:filename], "src/FrontCompiler") && return
contains(markers[:filename], "src/FFI") && return
Expand Down Expand Up @@ -531,7 +531,16 @@
is_safe_macro_call(y) =
y.head == :macrocall && y.args[1].head == :IDENTIFIER && y.args[1].val == "@safe"

is_safe_literal(x) = x.head in [:NOTHING, :INTEGER, :FLOAT, :TRUE, :FALSE]
is_safe_literal(x) = x.head in [:NOTHING,

Check failure on line 534 in src/linting/extended_checks.jl

View workflow job for this annotation

GitHub Actions / run_lint

Use `tin(item,collection)` instead of the Julia's `in` or `∈`.
:INTEGER,
:FLOAT,
:TRUE,
:FALSE,
:HEXINT,
:BININT,
:CHAR,
:OCTINT
]

for arg in x.args[2:end]
# This is safe
Expand Down
2 changes: 2 additions & 0 deletions test/rai_rules_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
result = String(take!(io))
all_lines = split(result, "\n")

verbose && @info result

Check failure on line 31 in test/rai_rules_tests.jl

View workflow job for this annotation

GitHub Actions / run_lint

Unsafe logging statement. You must enclose variables and strings with `@safe(...)`.
# We remove decorations
return length(filter(l->startswith(l, "Line "), all_lines))
end
Expand Down Expand Up @@ -1943,6 +1943,8 @@

@warnv 1 @safe("Safe logging")

@warnv 1 @safe("Safe logging with non-common literals") 0x12 'c' 0b0 0o0

@infov 1 @safe(
"[Compilation] \
Creating a new BeTreeV2 specialization: $(K) and $(V) where eps = $(E) \n\
Expand Down
Loading