Skip to content

Commit c697303

Browse files
committed
refactor(move): remove lifetime-container-move to simplify usage
1 parent c15489a commit c697303

File tree

5 files changed

+17
-99
lines changed

5 files changed

+17
-99
lines changed

README.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -445,17 +445,6 @@ void bar(std::vector<std::unique_ptr<int>>& cont, std::unique_ptr<int>& p)
445445
}
446446
```
447447
448-
You can use `-Wlifetime-container-move` to enable check for it. But it's always better to introduce a helper function to state your intent:
449-
450-
```cpp
451-
// pset(container) = {*container}
452-
move_each(std::move(container), [](auto&& elem){
453-
// use elem
454-
});
455-
// pset(container) = {invalid}
456-
assert(container.empty());
457-
```
458-
459448
# Difference from the original implementation
460449
## Output variable
461450
### Return check

include/cppsafe/Options.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ struct CppsafeOptions {
1010
bool LifetimeDisabled = false;
1111
bool LifetimeGlobal = false;
1212
bool LifetimeOutput = false;
13-
bool LifetimeContainerMove = false;
1413
};
1514

1615
}

integration_test/feature/move_container.cpp

Lines changed: 0 additions & 78 deletions
This file was deleted.

lib/lifetime/LifetimePsetBuilder.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -841,12 +841,24 @@ class PSetsBuilder final : public ConstStmtVisitor<PSetsBuilder, void>, public P
841841
}
842842

843843
if (PS.containsParent(V)) {
844+
// WORKAROUND: https://github.com/qqiangwu/cppsafe/issues/82
845+
//
844846
// when move var with pset {*container}, only invalidate the var itself,
845-
// containers and iterators are not invalidated
846-
if (!Reporter.getOptions().LifetimeContainerMove) {
847-
if (V.isDeref() && isIteratorOrContainer(Var.getType())) {
848-
continue;
849-
}
847+
// containers and iterators are not invalidated.
848+
// <code>
849+
// void foo(std::vector<std::unique_ptr<int>>& cont, std::unique_ptr<int>& p)
850+
//{
851+
// for (auto& x : cont) {
852+
// p = std::move(x);
853+
//}
854+
855+
// for (auto& x: cont) {
856+
// use(x);
857+
//}
858+
//}
859+
//</code>
860+
if (V.isDeref() && isIteratorOrContainer(Var.getType())) {
861+
continue;
850862
}
851863
setPSet(PSet::singleton(Var), PSet::invalid(Reason), Reason.getRange());
852864
}

src/main.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ static const cl::opt<bool> WarnLifetimeDisabled("Wlifetime-disabled",
6262
static const cl::opt<bool> WarnLifetimeOutput("Wlifetime-output",
6363
desc("Enforce output parameter validity check in all paths"), cl::init(false), cl::cat(CppSafeCategory));
6464

65-
static const cl::opt<bool> WarnLifetimeContainerMove("Wlifetime-container-move",
66-
desc("Enable strict check on element move of containers"), cl::init(false), cl::cat(CppSafeCategory));
67-
6865
struct DetectSystemIncludesError : public std::runtime_error {
6966
using std::runtime_error::runtime_error;
7067
};
@@ -132,7 +129,6 @@ class LifetimeFrontendAction : public clang::ASTFrontendAction {
132129
.LifetimeDisabled = WarnLifetimeDisabled,
133130
.LifetimeGlobal = WarnLifetimeGlobal,
134131
.LifetimeOutput = WarnLifetimeOutput,
135-
.LifetimeContainerMove = WarnLifetimeContainerMove,
136132
};
137133

138134
return std::make_unique<AstConsumer>(Options);

0 commit comments

Comments
 (0)