Skip to content

Commit 918fbbf

Browse files
committed
Dictionary::insert() is now #[must_use], to encourage set() if result is unused
1 parent 901d626 commit 918fbbf

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

godot-core/src/builtin/collections/dictionary.rs

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ impl Dictionary {
160160
/// Insert a value at the given key, returning the previous value for that key (if available).
161161
///
162162
/// If you don't need the previous value, use [`set()`][Self::set] instead.
163+
#[must_use]
163164
pub fn insert<K: ToGodot, V: ToGodot>(&mut self, key: K, value: V) -> Option<Variant> {
164165
let key = key.to_variant();
165166
let old_value = self.get(key.clone());

itest/rust/src/builtin_tests/containers/dictionary_test.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn dictionary_clone() {
101101

102102
#[allow(clippy::redundant_clone)]
103103
let clone = dictionary.clone();
104-
Dictionary::from_variant(&clone.get("bar").unwrap()).insert("final", 4);
104+
Dictionary::from_variant(&clone.get("bar").unwrap()).set("final", 4);
105105
assert_eq!(subdictionary.get("final"), Some(4.to_variant()));
106106
}
107107

@@ -147,7 +147,7 @@ fn dictionary_duplicate_deep() {
147147
"bar": subdictionary.clone()
148148
};
149149
let clone = dictionary.duplicate_deep();
150-
Dictionary::from_variant(&clone.get("bar").unwrap()).insert("baz", 4);
150+
Dictionary::from_variant(&clone.get("bar").unwrap()).set("baz", 4);
151151
assert_eq!(
152152
subdictionary.get("baz"),
153153
Some(true.to_variant()),
@@ -167,14 +167,14 @@ fn dictionary_duplicate_shallow() {
167167
};
168168

169169
let mut clone = dictionary.duplicate_shallow();
170-
Dictionary::from_variant(&clone.get("bar").unwrap()).insert("baz", 4);
170+
Dictionary::from_variant(&clone.get("bar").unwrap()).set("baz", 4);
171171
assert_eq!(
172172
subdictionary.get("baz"),
173173
Some(4.to_variant()),
174174
"key = \"baz\""
175175
);
176176

177-
clone.insert("foo", false.to_variant());
177+
clone.set("foo", false);
178178
assert_eq!(dictionary.get("foo"), Some(0.to_variant()));
179179
assert_eq!(clone.get("foo"), Some(false.to_variant()));
180180
}
@@ -256,13 +256,13 @@ fn dictionary_insert_multiple() {
256256
let mut dictionary = dict! {};
257257
assert!(dictionary.is_empty());
258258

259-
dictionary.insert(1, true);
259+
dictionary.set(1, true);
260260
assert_eq!(dictionary.get(1), Some(true.to_variant()));
261261

262262
let mut other = dict! {};
263263
assert!(other.is_empty());
264264

265-
other.insert(1, 2);
265+
other.set(1, 2);
266266
assert_eq!(other.get(1), Some(2.to_variant()));
267267
}
268268
#[itest]
@@ -430,7 +430,7 @@ fn dictionary_iter_size_hint() {
430430
assert_eq!(iter.size_hint(), (0, Some(0)));
431431

432432
// Insertion while iterating is allowed and might change size hint.
433-
dictionary_clone.insert("new_key", "soma_val");
433+
dictionary_clone.set("new_key", "soma_val");
434434
assert_eq!(iter.size_hint(), (1, Some(1)));
435435

436436
// Removal while iterating is also allowed and might change size_hint.
@@ -465,7 +465,9 @@ fn dictionary_iter_insert() {
465465
iter.next();
466466
iter.next();
467467

468-
dictionary2.insert("new_key", 10);
468+
let prev = dictionary2.insert("new_key", 10);
469+
assert_eq!(prev, None);
470+
469471
let v: Vec<_> = iter.collect();
470472
assert_eq!(dictionary.len(), 5);
471473
assert!(dictionary.contains_key("new_key"));
@@ -488,7 +490,7 @@ fn dictionary_iter_insert_after_completion() {
488490
}
489491
assert_eq!(iter.next(), None);
490492

491-
dictionary2.insert("new_key", 10);
493+
dictionary2.set("new_key", 10);
492494
assert_eq!(iter.next(), None);
493495
assert_eq!(dictionary.len(), 5);
494496
}
@@ -504,7 +506,7 @@ fn dictionary_iter_big() {
504506
for _ in 0..16 {
505507
iter.next();
506508
}
507-
dictionary2.insert("a", "b");
509+
dictionary2.set("a", "b");
508510
}
509511
dictionary2.clear();
510512
dictionary2.extend((0..64).zip(0..64));
@@ -531,7 +533,7 @@ fn dictionary_iter_simultaneous() {
531533
})
532534
.collect();
533535

534-
assert!(map.len() == 4);
536+
assert_eq!(map.len(), 4);
535537

536538
let mut tens = 0;
537539
let mut trues = 0;

0 commit comments

Comments
 (0)