Skip to content

Commit

Permalink
Try using noop-attr.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdm committed Dec 25, 2022
1 parent fbd9332 commit 816eaee
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 29 deletions.
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ edition = "2021"

[dependencies]
derive = { path = "../derive" }
lint = { path = "../lint" }
#lint = { path = "../lint" }
noop-attr = "0.1"
20 changes: 13 additions & 7 deletions app/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#[derive(derive::JSTraceable)]
struct S;
mod unrooted_must_root_lint {
pub use noop_attr::noop as must_root;
}

/*#[derive(derive::JSTraceable)]
struct S;*/

#[cfg_attr(dylint_lib = "lint", unrooted_must_root_lint::must_root)]
struct U;
#[unrooted_must_root_lint::must_root]
struct U {
_v: i32,
}

trait JSTraceable {
fn trace(&self) {}
Expand All @@ -13,8 +19,8 @@ fn foo<T: JSTraceable>(t: &T) {
}

fn main() {
let _u = U;
let s = S;
foo(&s);
let _u = U { _v: 0 };
//let s = S;
//foo(&s);
println!("Hello, world!");
}
2 changes: 1 addition & 1 deletion lint/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
publish = false

[lib]
crate-type = ["cdylib", "rlib"]
crate-type = ["cdylib"]

[dependencies]
#clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "f4850f7292efa33759b4f7f9b7621268979e9914" }
Expand Down
41 changes: 22 additions & 19 deletions lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ declare_lint!(
"Warn and report usage of unrooted jsmanaged objects"
);

/*dylint_linting::declare_late_lint! {
pub LINT,
Warn,
"description goes here"
}*/

pub(crate) struct UnrootedPass {
symbols: Symbols,
}
Expand All @@ -49,14 +43,21 @@ impl UnrootedPass {
}


fn has_lint_attr(sym: &Symbols, attrs: &[Attribute], name: Symbol) -> bool {
fn has_lint_attr(sym: &Symbols, attrs: &[Attribute]) -> bool {
if attrs.is_empty() {
return false;
}
match &attrs[0].kind {
AttrKind::Normal(normal) => println!("{:?} {:?} {:?}", normal.item.path, sym.unrooted_must_root_lint, sym.must_root),
_ => {},
}
attrs.iter().any(|attr| {
matches!(
&attr.kind,
AttrKind::Normal(normal)
if normal.item.path.segments.len() == 2 &&
normal.item.path.segments[0].ident.name == sym.unrooted_must_root_lint &&
normal.item.path.segments[1].ident.name == name
normal.item.path.segments[1].ident.name == sym.must_root
)
})
}
Expand All @@ -70,23 +71,25 @@ impl LintPass for UnrootedPass {
impl<'tcx> LateLintPass<'tcx> for UnrootedPass {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item) {
let attrs = cx.tcx.hir().attrs(item.hir_id());
if !has_lint_attr(&self.symbols, &attrs, self.symbols.must_root) {
println!("checking {:?} {:?} {:?}", item.ident, item.span, attrs);
if has_lint_attr(&self.symbols, &attrs) {
cx.lint(
UNROOTED_MUST_ROOT,
"Type must be rooted, use #[unrooted_must_root_lint::must_root] \
on the struct definition to propagate",
|_lint| _lint,
);
return;
}
if let hir::ItemKind::Struct(def, ..) = &item.kind {
for ref field in def.fields() {
//if let hir::ItemKind::Struct(def, ..) = &item.kind {
// for ref field in def.fields() {
//let def_id = cx.tcx.hir().local_def_id(field.hir_id);
//if true
//if is_unrooted_ty(&self.symbols, cx, cx.tcx.type_of(def_id), false) {
cx.lint(
UNROOTED_MUST_ROOT,
"Type must be rooted, use #[unrooted_must_root_lint::must_root] \
on the struct definition to propagate",
|lint| lint.set_span(field.span),
)
//|lint| lint.set_span(field.span),
//}
}
}
// }
//}
}
}

Expand Down

0 comments on commit 816eaee

Please sign in to comment.