Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
5660: COMP: offer only struct fields in struct pat r=mchernyavsky a=Kobzol

This PR filters out unnecessary completions offered in struct pats.
It also adds `..` to completion, but I'm not sure if that's the right way to do it, so it's in a separate commit.

Fixes third part of #4448

5766: INSP: attach module without a parent to a parent module in AttachFileToModuleFix r=mchernyavsky a=Kobzol

#5490 added a quick fix to attach a file to a nearby module. However, for `mod.rs` files it offered only crate root files (`lib.rs`, `main.rs`) and not parent modules. So in this case:

```rust
//- foo/mod.rs
/* nothing to see here */
//- foo/bar/mod.rs
/* caret*/
```
The quick fix wasn't offered at `/*caret*/`.

After this PR, the quick fix is offered and the example code is transformed into this:

```rust
//- foo/mod.rs
mod bar;
//- foo/bar/mod.rs
```

5811: INSP: add support for trait refs in RsWrongTypeArgumentsNumberInspection r=undin a=Kobzol

`RsWrongTypeArgumentsNumberInspection` previously did not work for trait refs, for example `dyn Trait` or `impl Trait`. This PR adds support for these cases.

TODO:
- [x] run `RsRealProjectAnalysisTest`

5816: Refactoring of auto-import related code r=mchernyavsky a=Undin

These changes extract auto-import related code from `AutoImportFix` into `org.rust.ide.utils.import` package since this code widely used in different IDE features.
Also, this code is split into several separate files

As a result, a lot of code doesn't depend on `AutoImportFix`


5819: REF: add unsafe modifier to functions extracted from unsafe functions r=mchernyavsky a=Kobzol

This PR adds `unsafe` to functions extracted from `unsafe` functions. Currently the `unsafe` is added unconditionally, should I improve that?

The extracted statements would have to be scanned for unsafe operations that are not inside an `unsafe` block. Since pretty much the same code is already in `RsUnsafeExpressionAnnotator`, it should be refactored somehow to share the logic.

Fixes: #5814

Co-authored-by: Jakub Beránek <berykubik@gmail.com>
Co-authored-by: Arseniy Pendryak <a.pendryak@yandex.ru>
  • Loading branch information
3 people committed Jul 28, 2020
6 parents 3928e2f + badc5ef + fb516f0 + 391fc33 + 01561f2 + 2a9b860 commit ec93641
Show file tree
Hide file tree
Showing 31 changed files with 778 additions and 638 deletions.
Expand Up @@ -12,9 +12,9 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import org.rust.cargo.project.workspace.PackageOrigin
import org.rust.ide.inspections.import.RsImportHelper.importTypeReferencesFromTy
import org.rust.ide.presentation.render
import org.rust.ide.presentation.renderInsertionSafe
import org.rust.ide.utils.import.RsImportHelper.importTypeReferencesFromTy
import org.rust.lang.core.psi.RsFunction
import org.rust.lang.core.psi.RsLambdaExpr
import org.rust.lang.core.psi.RsPsiFactory
Expand Down
Expand Up @@ -20,22 +20,29 @@ class RsWrongTypeArgumentsNumberInspection : RsLocalInspectionTool() {
override fun buildVisitor(holder: RsProblemsHolder, isOnTheFly: Boolean) =
object : RsVisitor() {
override fun visitBaseType(type: RsBaseType) {
// Don't apply generic declaration checks to Fn-traits and `Self`
if (type.path?.valueParameterList != null) return
if (type.path?.cself != null) return
checkMethod(holder, type)
if (!isPathValid(type.path)) return
checkTypeArguments(holder, type)
}

override fun visitCallExpr(o: RsCallExpr) = checkMethod(holder, o)
override fun visitMethodCall(o: RsMethodCall) = checkMethod(holder, o)
override fun visitTraitRef(trait: RsTraitRef) {
if (!isPathValid(trait.path)) return
checkTypeArguments(holder, trait)
}

override fun visitCallExpr(o: RsCallExpr) = checkTypeArguments(holder, o)
override fun visitMethodCall(o: RsMethodCall) = checkTypeArguments(holder, o)
}

private fun checkMethod(holder: RsProblemsHolder, o: RsElement) {
// Don't apply generic declaration checks to Fn-traits and `Self`
private fun isPathValid(path: RsPath?): Boolean = path?.valueParameterList == null && path?.cself == null

private fun checkTypeArguments(holder: RsProblemsHolder, o: RsElement) {
val (actualArguments, declaration) = when (o) {
is RsMethodCall -> o.typeArgumentList to o.reference.resolve()
is RsCallExpr ->
(o.expr as? RsPathExpr)?.path?.typeArgumentList to (o.expr as? RsPathExpr)?.path?.reference?.resolve()
is RsBaseType -> o.path?.typeArgumentList to o.path?.reference?.resolve()
is RsTraitRef -> o.path.typeArgumentList to o.path.reference?.resolve()
else -> return
}
if (declaration !is RsGenericDeclaration) return
Expand All @@ -48,7 +55,7 @@ class RsWrongTypeArgumentsNumberInspection : RsLocalInspectionTool() {
if (actualArgs == expectedTotalParams) return

val errorText = when (o) {
is RsBaseType -> checkBaseType(actualArgs, expectedRequiredParams, expectedTotalParams)
is RsBaseType, is RsTraitRef -> checkTypeReference(actualArgs, expectedRequiredParams, expectedTotalParams)
is RsMethodCall, is RsCallExpr -> checkFunctionCall(actualArgs, expectedRequiredParams, expectedTotalParams)
else -> null
} ?: return
Expand All @@ -60,7 +67,7 @@ class RsWrongTypeArgumentsNumberInspection : RsLocalInspectionTool() {
}
}

private fun checkBaseType(actualArgs: Int, expectedRequiredParams: Int, expectedTotalParams: Int): String? {
private fun checkTypeReference(actualArgs: Int, expectedRequiredParams: Int, expectedTotalParams: Int): String? {
return when {
actualArgs > expectedTotalParams ->
if (expectedRequiredParams != expectedTotalParams) "at most $expectedTotalParams" else "$expectedTotalParams"
Expand Down
Expand Up @@ -10,7 +10,7 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import org.rust.ide.inspections.checkMatch.Pattern
import org.rust.ide.inspections.import.RsImportHelper.importTypeReferencesFromTy
import org.rust.ide.utils.import.RsImportHelper.importTypeReferencesFromTy
import org.rust.lang.core.psi.RsBlockExpr
import org.rust.lang.core.psi.RsMatchExpr
import org.rust.lang.core.psi.RsPsiFactory
Expand Down
Expand Up @@ -77,6 +77,9 @@ class AttachFileToModuleFix(
val modules = mutableListOf<RsMod>()

if (file.isModuleFile) {
// module file in parent directory
modules.addIfNotNull(findModule(file, project, directory.parent?.findFileByRelativePath(RsConstants.MOD_RS_FILE)))

// package target roots in parent directory
for (target in pkg.targets) {
val crateRoot = target.crateRoot ?: continue
Expand All @@ -90,8 +93,7 @@ class AttachFileToModuleFix(

// module file in parent directory
if (pkg.edition == CargoWorkspace.Edition.EDITION_2018) {
val parent = directory.parent
modules.addIfNotNull(findModule(file, project, parent?.findFileByRelativePath("${directory.name}.rs")))
modules.addIfNotNull(findModule(file, project, directory.parent?.findFileByRelativePath("${directory.name}.rs")))
}

// package target roots in the same directory
Expand Down

0 comments on commit ec93641

Please sign in to comment.