Skip to content

Commit 1f22944

Browse files
committed
Remove more clones
1 parent 8492b53 commit 1f22944

9 files changed

Lines changed: 27 additions & 28 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ bool Converter::RecordDerivesDefault(const clang::RecordDecl *decl) {
669669
return true;
670670
}
671671

672-
bool Converter::RecordDerivesCopy(const clang::RecordDecl *decl) {
672+
bool Converter::RecordDerivesCopy(const clang::RecordDecl *decl) const {
673673
auto *derives = Mapper::MappedDerives(ctx_.getCanonicalTagType(decl));
674674
return derives &&
675675
std::find(derives->begin(), derives->end(), "Copy") != derives->end();
@@ -4394,15 +4394,8 @@ void Converter::SetFresh() {
43944394
}
43954395
}
43964396

4397-
static bool hasCopyTrait(clang::QualType type) {
4398-
if (type->isBuiltinType())
4399-
return true;
4400-
4401-
return false;
4402-
}
4403-
44044397
void Converter::SetValueFreshness(clang::QualType type) {
4405-
if (hasCopyTrait(type)) {
4398+
if (TypeIsCopyable(type)) {
44064399
computed_expr_type_ = ComputedExprType::FreshValue;
44074400
} else if (type->isPointerType() || type->isReferenceType()) {
44084401
computed_expr_type_ = ComputedExprType::Pointer;

‎cpp2rust/converter/converter.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,13 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
279279
if (ty->isFunctionPointerType() || ty->isFunctionType()) {
280280
return FunctionPointerImplementsCopy();
281281
}
282-
return ty->isIntegerType();
282+
if (ty->isBuiltinType() || ty->isEnumeralType()) {
283+
return true;
284+
}
285+
if (auto *record = ty->getAsRecordDecl()) {
286+
return RecordDerivesCopy(record);
287+
}
288+
return false;
283289
}
284290

285291
virtual void ConvertPrintf(clang::CallExpr *expr);
@@ -602,7 +608,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
602608

603609
virtual bool RecordDerivesDefault(const clang::RecordDecl *decl);
604610

605-
bool RecordDerivesCopy(const clang::RecordDecl *decl);
611+
bool RecordDerivesCopy(const clang::RecordDecl *decl) const;
606612

607613
bool RecordHasCopyableFields(const clang::RecordDecl *decl);
608614

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2391,7 +2391,7 @@ void ConverterRefCount::ConvertDeref(clang::Expr *expr) {
23912391
Convert(expr);
23922392
if (deref) {
23932393
StrCat(GetPointerDerefSuffix(pointee_type));
2394-
SetValueFreshness(expr->getType());
2394+
SetValueFreshness(pointee_type);
23952395
}
23962396
}
23972397

‎tests/multi-file/cross_tu_tag_collision/out/refcount/cross_tu_tag_collision.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,5 @@ impl ByteRepr for widget_enum {
7171
}
7272
pub fn b_value_1() -> i32 {
7373
let w: Value<widget_enum> = Rc::new(RefCell::new(widget_enum::WIDGET_C));
74-
return ((*w.borrow()) as i32).clone();
74+
return ((*w.borrow()) as i32);
7575
}

‎tests/unit/out/refcount/anonymous_enum.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl Clone for WithAnonField {
136136
fn clone(&self) -> Self {
137137
let mut this = Self {
138138
a: Rc::new(RefCell::new((*self.a.borrow()))),
139-
field: Rc::new(RefCell::new((*self.field.borrow()).clone())),
139+
field: Rc::new(RefCell::new((*self.field.borrow()))),
140140
};
141141
this
142142
}

‎tests/unit/out/refcount/bool_condition_enum.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn main_0() -> i32 {
5252
}
5353
let t9: Value<i32> = Rc::new(RefCell::new((!((*code.borrow()) != Code::from(0)) as i32)));
5454
assert!(((*t9.borrow()) == 1));
55-
let b4: Value<bool> = Rc::new(RefCell::new(((*code.borrow()) != Code::from(0)).clone()));
55+
let b4: Value<bool> = Rc::new(RefCell::new(((*code.borrow()) != Code::from(0))));
5656
assert!(!(*b4.borrow()));
5757
return 0;
5858
}

‎tests/unit/out/refcount/bool_condition_enum_c.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn main_0() -> i32 {
5252
}
5353
let t9: Value<i32> = Rc::new(RefCell::new((!((*code.borrow()) != Code::from(0)) as i32)));
5454
assert!(((((*t9.borrow()) == 1) as i32) != 0));
55-
let b4: Value<bool> = Rc::new(RefCell::new(((*code.borrow()) != Code::from(0)).clone()));
55+
let b4: Value<bool> = Rc::new(RefCell::new(((*code.borrow()) != Code::from(0))));
5656
assert!(((!(*b4.borrow()) as i32) != 0));
5757
return 0;
5858
}

‎tests/unit/out/refcount/enum_int_interop.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ impl Clone for Entry {
9696
fn clone(&self) -> Self {
9797
let mut this = Self {
9898
name: Rc::new(RefCell::new((*self.name.borrow()).clone())),
99-
color: Rc::new(RefCell::new((*self.color.borrow()).clone())),
100-
opt: Rc::new(RefCell::new((*self.opt.borrow()).clone())),
99+
color: Rc::new(RefCell::new((*self.color.borrow()))),
100+
opt: Rc::new(RefCell::new((*self.opt.borrow()))),
101101
};
102102
this
103103
}
@@ -149,7 +149,7 @@ thread_local!(
149149
);
150150
pub fn as_int_4(c: Color) -> i32 {
151151
let c: Value<Color> = Rc::new(RefCell::new(c));
152-
return ((*c.borrow()) as i32).clone();
152+
return ((*c.borrow()) as i32);
153153
}
154154
pub fn classify_option_5(option: i32) -> i32 {
155155
let option: Value<i32> = Rc::new(RefCell::new(option));
@@ -207,7 +207,7 @@ fn main_0() -> i32 {
207207
}
208208
}
209209
};
210-
let x: Value<i32> = Rc::new(RefCell::new(((*c.borrow()) as i32).clone()));
210+
let x: Value<i32> = Rc::new(RefCell::new(((*c.borrow()) as i32)));
211211
assert!(((*x.borrow()) == 0));
212212
let y: Value<i32> = Rc::new(RefCell::new((((*c.borrow()) as i32) + 1)));
213213
assert!(((*y.borrow()) == 1));
@@ -221,12 +221,12 @@ fn main_0() -> i32 {
221221
let o: Value<Option> = Rc::new(RefCell::new(Option::OPT_A));
222222
assert!((((*o.borrow()) as i32) == (Option::OPT_A as i32)));
223223
assert!((((*o.borrow()) as i32) == 10));
224-
let oi: Value<i32> = Rc::new(RefCell::new(((*o.borrow()) as i32).clone()));
224+
let oi: Value<i32> = Rc::new(RefCell::new(((*o.borrow()) as i32)));
225225
assert!(((*oi.borrow()) == 10));
226226
(*o.borrow_mut()) = Option::from(20);
227227
assert!((((*o.borrow()) as i32) == (Option::OPT_B as i32)));
228228
let rc: Value<i32> = Rc::new(RefCell::new(
229-
({ classify_option_5(((*o.borrow()) as i32).clone()) }),
229+
({ classify_option_5(((*o.borrow()) as i32)) }),
230230
));
231231
assert!(((*rc.borrow()) == 2));
232232
(*rc.borrow_mut()) = ({ classify_option_5(20) });
@@ -236,7 +236,7 @@ fn main_0() -> i32 {
236236
let t: Value<Tag> = Rc::new(RefCell::new(Tag::TAG_ONE));
237237
assert!((((*t.borrow()) as i32) == 1));
238238
assert!((((*t.borrow()) as i32) == (Tag::TAG_ONE as i32)));
239-
let ti: Value<i32> = Rc::new(RefCell::new(((*t.borrow()) as i32).clone()));
239+
let ti: Value<i32> = Rc::new(RefCell::new(((*t.borrow()) as i32)));
240240
assert!(((*ti.borrow()) == 1));
241241
(*t.borrow_mut()) = Tag::from(2);
242242
assert!((((*t.borrow()) as i32) == (Tag::TAG_TWO as i32)));

‎tests/unit/out/refcount/enum_int_interop_c.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ thread_local!(
148148
);
149149
pub fn as_int_4(c: Color) -> i32 {
150150
let c: Value<Color> = Rc::new(RefCell::new(c));
151-
return ((*c.borrow()) as i32).clone();
151+
return ((*c.borrow()) as i32);
152152
}
153153
pub fn classify_option_5(option: i32) -> i32 {
154154
let option: Value<i32> = Rc::new(RefCell::new(option));
@@ -206,7 +206,7 @@ fn main_0() -> i32 {
206206
}
207207
}
208208
};
209-
let x: Value<i32> = Rc::new(RefCell::new(((*c.borrow()) as i32).clone()));
209+
let x: Value<i32> = Rc::new(RefCell::new(((*c.borrow()) as i32)));
210210
assert!(((((*x.borrow()) == 0) as i32) != 0));
211211
let y: Value<i32> = Rc::new(RefCell::new(
212212
((((*c.borrow()) as u32).wrapping_add(1_u32)) as i32),
@@ -224,12 +224,12 @@ fn main_0() -> i32 {
224224
let o: Value<Option> = Rc::new(RefCell::new(Option::OPT_A));
225225
assert!((((((*o.borrow()) as u32) == ((Option::OPT_A as i32) as u32)) as i32) != 0));
226226
assert!((((((*o.borrow()) as u32) == 10_u32) as i32) != 0));
227-
let oi: Value<i32> = Rc::new(RefCell::new(((*o.borrow()) as i32).clone()));
227+
let oi: Value<i32> = Rc::new(RefCell::new(((*o.borrow()) as i32)));
228228
assert!(((((*oi.borrow()) == 10) as i32) != 0));
229229
(*o.borrow_mut()) = Option::from(20);
230230
assert!((((((*o.borrow()) as u32) == ((Option::OPT_B as i32) as u32)) as i32) != 0));
231231
let rc: Value<i32> = Rc::new(RefCell::new(
232-
({ classify_option_5(((*o.borrow()) as i32).clone()) }),
232+
({ classify_option_5(((*o.borrow()) as i32)) }),
233233
));
234234
assert!(((((*rc.borrow()) == 2) as i32) != 0));
235235
(*rc.borrow_mut()) = ({ classify_option_5(20) });
@@ -239,7 +239,7 @@ fn main_0() -> i32 {
239239
let t: Value<Tag_enum> = Rc::new(RefCell::new(Tag_enum::TAG_ONE));
240240
assert!((((((*t.borrow()) as u32) == 1_u32) as i32) != 0));
241241
assert!((((((*t.borrow()) as u32) == ((Tag_enum::TAG_ONE as i32) as u32)) as i32) != 0));
242-
let ti: Value<i32> = Rc::new(RefCell::new(((*t.borrow()) as i32).clone()));
242+
let ti: Value<i32> = Rc::new(RefCell::new(((*t.borrow()) as i32)));
243243
assert!(((((*ti.borrow()) == 1) as i32) != 0));
244244
(*t.borrow_mut()) = Tag_enum::from(2);
245245
assert!((((((*t.borrow()) as u32) == ((Tag_enum::TAG_TWO as i32) as u32)) as i32) != 0));

0 commit comments

Comments
 (0)