File tree Expand file tree Collapse file tree 1 file changed +11
-11
lines changed Expand file tree Collapse file tree 1 file changed +11
-11
lines changed Original file line number Diff line number Diff line change @@ -113,6 +113,13 @@ Avoid preconditions that span across function boundaries:
113
113
114
114
``` rust
115
115
// Good
116
+ fn main () {
117
+ let s : & str = ... ;
118
+ if let Some (contents ) = string_literal_contents (s ) {
119
+
120
+ }
121
+ }
122
+
116
123
fn string_literal_contents (s : & str ) -> Option <& str > {
117
124
if s . starts_with ('"' ) && s . ends_with ('"' ) {
118
125
Some (& s [1 .. s . len () - 1 ])
@@ -121,24 +128,17 @@ fn string_literal_contents(s: &str) -> Option<&str> {
121
128
}
122
129
}
123
130
124
- fn foo () {
131
+ // Not as good
132
+ fn main () {
125
133
let s : & str = ... ;
126
- if let Some ( contents ) = string_literal_contents (s ) {
127
-
134
+ if is_string_literal (s ) {
135
+ let contents = & s [ 1 .. s . len () - 1 ];
128
136
}
129
137
}
130
138
131
- // Not as good
132
139
fn is_string_literal (s : & str ) -> bool {
133
140
s . starts_with ('"' ) && s . ends_with ('"' )
134
141
}
135
-
136
- fn foo () {
137
- let s : & str = ... ;
138
- if is_string_literal (s ) {
139
- let contents = & s [1 .. s . len () - 1 ];
140
- }
141
- }
142
142
```
143
143
144
144
In the "Not as good" version, the precondition that ` 1 ` is a valid char boundary is checked in ` is_string_literal ` and used in ` foo ` .
You can’t perform that action at this time.
0 commit comments