Skip to content
This repository was archived by the owner on Feb 18, 2022. It is now read-only.

Commit f1462b1

Browse files
committed
Allowed struct and enums if not including references
1 parent 8fa8851 commit f1462b1

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

clippy_lints/src/hacspec.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
use crate::utils::*;
1+
//! Checks syntax of programs written in the 'hacspec' subset of rust
2+
//! See https://github.com/hacspec/hacspec-rust/blob/franziskus-dev/LANGUAGE.md for the (BNF ?) syntax
3+
//!
4+
//!
5+
6+
7+
use crate::utils::span_lint;
8+
//might use crate::utils::higher to look into loops
29
use rustc::lint::in_external_macro;
310
use rustc_hir::{
4-
intravisit, BindingAnnotation, Body, Expr, ExprKind, FnDecl, HirId, Item, ItemKind, Mod, Mutability, Param, Pat,
5-
PatKind, Path, PathSegment, Ty, TyKind,
11+
intravisit, BindingAnnotation, Body, Expr, ExprKind, FnDecl, HirId, Item, ItemKind, Mod, Param, Pat,
12+
PatKind, Path, PathSegment, Ty, TyKind, StructField,
613
};
714
use rustc_lint::{LateContext, LateLintPass, LintContext};
815
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -13,7 +20,7 @@ use rustc_span::{
1320

1421
declare_clippy_lint! {
1522
pub HACSPEC,
16-
pedantic,
23+
nursery, //pedantic, corectness or restriction, but shouldn't interfere with other lints ? or shipped completely separately
1724
"Checks whether the code belongs to the hacspec subset of Rust"
1825
}
1926

@@ -56,7 +63,10 @@ fn allowed_type(typ: &Ty<'_>) -> bool {
5663
}
5764
}
5865

66+
67+
5968
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Hacspec {
69+
6070
fn check_path(&mut self, cx: &LateContext<'a, 'tcx>, path: &'tcx Path<'tcx>, _: HirId) {
6171
// Items used in the code are whitelisted
6272
if in_external_macro(cx.sess(), path.span) {
@@ -132,22 +142,31 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Hacspec {
132142
}
133143
}
134144

145+
fn check_struct_field(
146+
&mut self,
147+
cx: &LateContext<'a, 'tcx>,
148+
sf: &'tcx StructField<'tcx>
149+
) {
150+
// The types of struct (incl. tuples) declaration parameters cannot be references
151+
if !(allowed_type(sf.ty)) {
152+
span_lint(cx, HACSPEC, sf.span, &"[HACSPEC] Unsupported type")
153+
}
154+
}
155+
156+
135157
fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item<'tcx>) {
136158
if in_external_macro(cx.sess(), item.span) {
137159
return;
138160
}
139161
match &item.kind {
140162
ItemKind::TyAlias(ref typ, _) | ItemKind::Const(ref typ, _) => {
141163
if !allowed_type(typ) {
142-
span_lint(cx, HACSPEC, item.span, &"[HACSPEC] Unauthorized type for alias")
143-
}
144-
},
145-
ItemKind::Static(typ, m, _) => {
146-
if !allowed_type(typ) || *m == Mutability::Mut {
147-
span_lint(cx, HACSPEC, item.span, &"[HACSPEC] Unauthorized static item")
164+
span_lint(cx, HACSPEC, item.span, &"[HACSPEC] Unsupported type")
148165
}
149166
},
150-
ItemKind::ExternCrate(_) | ItemKind::Use(_, _) | ItemKind::Fn(_, _, _) => (),
167+
ItemKind::Enum(_, _) | ItemKind::Struct(_, _) |
168+
ItemKind::Fn(_, _, _) |
169+
ItemKind::ExternCrate(_) | ItemKind::Use(_, _) => (),
151170
_ => span_lint(cx, HACSPEC, item.span, &"[HACSPEC] Unauthorized item"),
152171
}
153172
}

0 commit comments

Comments
 (0)