mirror of
				https://github.com/rust-lang/rust-analyzer.git
				synced 2025-11-03 13:13:18 +00:00 
			
		
		
		
	minor
This commit is contained in:
		
							parent
							
								
									6976494781
								
							
						
					
					
						commit
						1688e481b3
					
				@ -113,6 +113,13 @@ Avoid preconditions that span across function boundaries:
 | 
			
		||||
 | 
			
		||||
```rust
 | 
			
		||||
// Good
 | 
			
		||||
fn main() {
 | 
			
		||||
    let s: &str = ...;
 | 
			
		||||
    if let Some(contents) = string_literal_contents(s) {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn string_literal_contents(s: &str) -> Option<&str> {
 | 
			
		||||
    if s.starts_with('"') && s.ends_with('"') {
 | 
			
		||||
        Some(&s[1..s.len() - 1])
 | 
			
		||||
@ -121,24 +128,17 @@ fn string_literal_contents(s: &str) -> Option<&str> {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn foo() {
 | 
			
		||||
    let s: &str = ...;
 | 
			
		||||
    if let Some(contents) = string_literal_contents(s) {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Not as good
 | 
			
		||||
fn is_string_literal(s: &str) -> bool {
 | 
			
		||||
    s.starts_with('"') && s.ends_with('"')
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn foo() {
 | 
			
		||||
fn main() {
 | 
			
		||||
    let s: &str = ...;
 | 
			
		||||
    if is_string_literal(s) {
 | 
			
		||||
        let contents = &s[1..s.len() - 1];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn is_string_literal(s: &str) -> bool {
 | 
			
		||||
    s.starts_with('"') && s.ends_with('"')
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
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`.
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user