-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathbuiltin_macros.rs
More file actions
89 lines (81 loc) · 2.96 KB
/
builtin_macros.rs
File metadata and controls
89 lines (81 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Portions Copyright 2021-2025 Technology Concepts & Design, Inc.
All rights reserved.
Use of this source code is governed by the PostgreSQL license that can be found in the LICENSE.md file.
*/
use super::utils;
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_span::{Span, Symbol};
declare_plrust_lint!(
pub(crate) PLRUST_FILESYSTEM_MACROS,
"Disallow `include_str!`, and `include_bytes!`",
);
declare_plrust_lint!(
pub(crate) PLRUST_ENV_MACROS,
"Disallow `env!`, and `option_env!`",
);
rustc_lint_defs::declare_lint_pass!(PlrustBuiltinMacros => [PLRUST_FILESYSTEM_MACROS]);
impl PlrustBuiltinMacros {
fn lint_fs(&self, cx: &LateContext<'_>, sp: Span) {
cx.lint(
PLRUST_FILESYSTEM_MACROS,
"the `include_str`, `include_bytes`, and `include` macros are forbidden in PL/Rust",
|b| b.set_span(sp),
);
}
fn lint_env(&self, cx: &LateContext<'_>, sp: Span) {
cx.lint(
PLRUST_ENV_MACROS,
"the `env` and `option_env` macros are forbidden",
|b| b.set_span(sp),
);
}
fn check_span(&mut self, cx: &LateContext<'_>, span: Span) {
let fs_diagnostic_items = [
sym!(include_str_macro),
sym!(include_bytes_macro),
sym!(include_macro),
];
if let Some((s, ..)) = utils::check_span_against_macro_diags(cx, span, &fs_diagnostic_items)
{
self.lint_fs(cx, s);
return;
}
let fs_def_paths: &[&[Symbol]] = &[
&[sym!(core), sym!(macros), sym!(builtin), sym!(include)],
&[sym!(core), sym!(macros), sym!(builtin), sym!(include_bytes)],
&[sym!(core), sym!(macros), sym!(builtin), sym!(include_str)],
];
if let Some((s, ..)) = utils::check_span_against_macro_def_paths(cx, span, &fs_def_paths) {
self.lint_fs(cx, s);
return;
}
let env_diagnostic_items = [sym!(env_macro), sym!(option_env_macro)];
if let Some((s, ..)) =
utils::check_span_against_macro_diags(cx, span, &env_diagnostic_items)
{
self.lint_env(cx, s);
return;
}
let env_def_paths: &[&[Symbol]] = &[
&[sym!(core), sym!(macros), sym!(builtin), sym!(env)],
&[sym!(core), sym!(macros), sym!(builtin), sym!(option_env)],
];
if let Some((s, ..)) = utils::check_span_against_macro_def_paths(cx, span, &env_def_paths) {
self.lint_env(cx, s);
return;
}
}
}
impl<'tcx> LateLintPass<'tcx> for PlrustBuiltinMacros {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &hir::Item) {
self.check_span(cx, item.span)
}
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &hir::Stmt) {
self.check_span(cx, stmt.span)
}
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr) {
self.check_span(cx, expr.span)
}
}