Skip to content

Commit fd5ba46

Browse files
Undinyopox
authored andcommitted
ANN: annotate unsafe module and module declarations
1 parent bab0144 commit fd5ba46

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/main/kotlin/org/rust/ide/annotator/RsSyntaxErrorsAnnotator.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class RsSyntaxErrorsAnnotator : AnnotatorBase() {
3838
is RsStructItem -> checkStructItem(holder, element)
3939
is RsTypeAlias -> checkTypeAlias(holder, element)
4040
is RsConstant -> checkConstant(holder, element)
41+
is RsModItem -> checkModItem(holder, element)
42+
is RsModDeclItem -> checkModDeclItem(holder, element)
4143
}
4244
}
4345
is RsMacro -> checkMacro(holder, element)
@@ -320,6 +322,20 @@ private fun checkExternAbi(holder: AnnotationHolder, element: RsExternAbi) {
320322
}
321323
}
322324

325+
private fun checkModDeclItem(holder: AnnotationHolder, element: RsModDeclItem) {
326+
checkUnsafeModule(holder, element.unsafe)
327+
}
328+
329+
private fun checkModItem(holder: AnnotationHolder, element: RsModItem) {
330+
checkUnsafeModule(holder, element.unsafe)
331+
}
332+
333+
private fun checkUnsafeModule(holder: AnnotationHolder, unsafe: PsiElement?) {
334+
if (unsafe != null) {
335+
holder.newAnnotation(HighlightSeverity.ERROR, "Module cannot be declared unsafe").range(unsafe).create()
336+
}
337+
}
338+
323339
private enum class TypeKind {
324340
LIFETIME,
325341
TYPE,

src/test/kotlin/org/rust/ide/annotator/RsSyntaxErrorsAnnotatorTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,16 @@ class RsSyntaxErrorsAnnotatorTest : RsAnnotatorTestBase(RsSyntaxErrorsAnnotator:
407407
type ExternFnWithRawAbi = extern r"system" fn();
408408
type ExternFnWithInvalidAbi = extern <error descr="Non-string ABI literal">true</error> fn();
409409
""")
410+
411+
fun `test unsafe module`() = checkErrors("""
412+
mod module {}
413+
pub mod pub_module {}
414+
<error descr="Module cannot be declared unsafe">unsafe</error> mod unsafe_module {}
415+
pub <error descr="Module cannot be declared unsafe">unsafe</error> mod unsafe_module {}
416+
417+
mod mod_delc;
418+
pub mod pub_mmod_delc;
419+
<error descr="Module cannot be declared unsafe">unsafe</error> mod unsafe_mod_delc;
420+
pub <error descr="Module cannot be declared unsafe">unsafe</error> mod unsafe_mod_delc;
421+
""")
410422
}

0 commit comments

Comments
 (0)