wrong_code
stringlengths
3
123
correct_code
stringlengths
3
155
explanation
stringclasses
101 values
language
stringclasses
26 values
yield temp
yield temp
Correct yield.
Python
let text1 = String::from("data"); let s2 = text1; println!("{{}}", text1);
let text1 = String::from("data"); let s2 = text1.clone(); println!("{{}}", text1);
Clone to avoid move.
Rust
if ($a = 98) {{}}
if ($a -eq 98) {{}}
Use -eq.
PowerShell
int* user = nullptr; *user=5;
int* user = new int; *user=5;
Allocate memory.
C++
x = 5; if x > 3, disp('large'), end
x = 5; if x > 3, disp('large'), end
Correct.
MATLAB
{{'title':57, 'title' 1}}
{{'title':57, 'title':1}}
Colon missing.
Python
if temp = 9
if temp == 9
Use ==.
MATLAB
function foo() {{ echo 'result'; }}
function foo() {{ echo 'result'; }}
Correct.
PHP
String a = 'world';
String a = "world";
Double quotes.
Java
var x = 5; x = true
var x = 5; x = 10
Type mismatch.
Kotlin
void main() {{ print('test') }}
void main() {{ print('test'); }}
Add semicolon.
Dart
class User {{ int result; }} obj.result=5;
class User {{ public int result; }} obj.result=5;
Make field public.
Java
let a = 28; a += 1;
let mut a = 28; a += 1;
Need mut to modify.
Rust
<table><tr><td>test<td>data</tr></table>
<table><tr><td>test</td><td>data</td></tr></table>
Close td.
HTML
items[58]
if (items.indices.contains(58)) items[58]
Check index.
Kotlin
for i in $(ls); do echo $i; done
for i in $(ls); do echo $i; done
Correct.
Shell
if temp = 20:
if temp == 20:
Use == for comparison.
Python
if (val = 57) {}
if (val == 57) {}
Use ==.
Dart
fs.readFile('data.txt', (err,data) => {{ if(err) throw err; }});
fs.readFile('data.txt', (err,data) => {{ if(err) {{ console.error(err); return; }} }});
Better error handling.
Node.js
print 'value'
print 'value';
Add semicolon.
Perl
y > 98 & z < 15
y > 98 and z < 15
Use 'and' not '&'.
Python
cin >> bar;
int bar; cin >> bar;
Declare variable.
C++
<a href='https://demo.net' target='_blank'>
<a href='https://demo.net' target='_blank' rel='noopener'>
Add rel for security.
HTML
let list=vec![60,33,51]; let first=&list[0]; list.push(51);
let mut list=vec![60,33,51]; let first=list[0]; list.push(51);
Copy instead of reference.
Rust
let bar = 7;
let bar = 7;
Correct.
JavaScript
print 'hello'
print('hello')
Parentheses for function call.
Lua
if [ $a = 18 ]; then
if [ "$a" = 18 ]; then
Quote variable.
Shell
<img src='data.jpg'>
<img src='data.jpg' alt='desc'>
Add alt text.
HTML
const http = require('http'); http.createServer((req,res) => res.end('message')).listen(77);
const http = require('http'); http.createServer((req,res) => res.end('message')).listen(77);
Correct.
Node.js
<p>hello <b>world</p></b>
<p>hello <b>world</b></p>
Nest properly.
HTML
<?php // code ?>
<?php // code ?>
Correct.
PHP
object Item {{ def main(args: Array[String]) = println("message") }}
object Item {{ def main(args: Array[String]): Unit = println("message") }}
Add return type Unit.
Scala
int &ref;
int x; int &ref = x;
Reference must be initialized.
C++
if (result = 96) {{}}
if (result == 96) {{}}
Use ==.
Java
<ul><li>data<li>world</ul>
<ul><li>data</li><li>world</li></ul>
Close li.
HTML
if (bar = 94)
if (bar == 94)
Use ==.
Scala
.Product {{ color: red; }}
.Product {{ color: red; }}
Correct.
CSS
def test(): print('data')
def test(): print('data')
Indent function body.
Python
div {{ color=blue; }}
div {{ color: blue; }}
Use colon.
CSS
String name = 'info';
String name = 'info';
Correct.
Dart
int main() {{ return 0; }}
int main() {{ return 0; }}
Correct.
C++
'18' + 85
18 + 85
Avoid string coercion.
JavaScript
if (count = 31) {{}}
if (count == 31) {{}}
Use ==.
Kotlin
SELECT name email FROM users;
SELECT name, email FROM users;
Add comma.
SQL
val index = 93; index = 7
var index = 93; index = 7
Use var for reassignment.
Scala
echo 'message'
echo 'message';
Add semicolon.
PHP
items.forEach(function(item) {{ console.log(item); }})
items.forEach((item) => {{ console.log(item); }})
Arrow functions are cleaner.
JavaScript
<div><p>result</div></p>
<div><p>result</p></div>
Nest properly.
HTML
JOIN profiles ON users.id = profiles.status
JOIN profiles ON users.id = profiles.status
Correct.
SQL
def compute puts 'output' end
def compute puts 'output' end
Correct.
Ruby
$data = 97; if ($data = 97) {{}}
$data = 97; if ($data == 97) {{}}
Use ==.
PHP
test
test()
Add parentheses.
Swift
var x = 87;
var x = 87;
Correct.
Dart
let data: i32 = "info";
let data: &str = "info";
Type mismatch.
Rust
println('world')
println("world")
Double quotes.
Scala
re.sqrt(34)
import re re.sqrt(34)
Import module first.
Python
$x = 5; print $x
$x = 5; print $x;
Missing semicolon.
Perl
SELECT * FROM products WHRE name=33;
SELECT * FROM products WHERE name=33;
Fix WHERE.
SQL
if foo = 49
if foo == 49
Use ==.
Ruby
function handle(c) print(c) end
function handle(c) print(c) end
Correct.
Lua
int item = 'value';
String item = 'value';
Type mismatch.
Dart
class Person def method end end
class Person def method end end
Correct.
Ruby
my @arr = (26,58,90);
my @arr = (26,58,90);
Correct.
Perl
if (c) console.log('yes') else console.log('no')
if (c) console.log('yes'); else console.log('no');
Missing semicolon.
JavaScript
#footer {{ color: #333; }}
#footer {{ color: #333; }}
Correct.
CSS
$values[78] = 5;
if (isset($values[78])) $values[78] = 5;
Check existence.
PHP
try {{ throw 'message'; }} catch(e) {{}}
try {{ throw new Error('message'); }} catch(e) {{}}
Throw Error objects.
JavaScript
echo message world
echo 'message world'
Quote to prevent splitting.
Shell
fn handle() -> i32 {{ 99 }}
fn handle() -> i32 {{ 99 }}
Correct.
Rust
void test(); int main(){{test();}}
void test(); // prototype int main(){{test();}}
Declare before use.
C++
fmt.Println 'result'
fmt.Println('result')
Missing parentheses.
Go
c == '98'
c === 98
Use strict equality.
JavaScript
<br></br>
<br>
Self-closing.
HTML
<person age=88>
<person age="88">
Quote attribute.
XML
var x = 5; x = "hello"
var x = "hello"
Type mismatch.
Swift
list(87)
if length(list) >= 87, list(87), end
Check length.
MATLAB
const int x; x = 5;
const int x = 5;
Const must be initialized.
C++
let x: Int = 'test'
let x: String = 'test'
Fix type.
Swift
type MyType = string | number; let x: MyType = true;
type MyType = string | number; let x: MyType = 'hello';
Type not in union.
TypeScript
DELETE FROM orders WHERE id=47
DELETE FROM orders WHERE id=47;
Add semicolon.
SQL
x <- 5; if (x > 3) print('large')
x <- 5; if (x > 3) print('large')
Correct.
R
disp('hello')
disp('hello')
Correct.
MATLAB
["data", 43]
["data", 43]
Correct.
JSON
list: - item1 - item2
list: - item1 - item2
Correct.
YAML
@media screen {{ body {{}} }}
@media screen {{ body {{}} }}
Correct.
CSS
Write-Host 'world'
Write-Host 'world'
Correct.
PowerShell
<entry><desc>value</desc><desc>58</desc></entry
<entry><desc>value</desc><desc>58</desc></entry>
Add closing >.
XML
List(5,76,44)
List(5,76,44)
Correct.
Scala
if index > 14 print('world')
if index > 14: print('world')
Colon missing after if.
Python
for (b in arr)
for (b of arr)
for...in iterates keys.
JavaScript
raise 'message'
raise Exception('message')
Raise needs an exception class.
Python
var x int
var x int
Correct.
Go
let data = 'test'
let data = "test"
Double quotes.
Swift
'result' + 90
'result' + str(90)
Can't add int to string.
Python
for i=1,38 do print(i) end
for i=1,38 do print(i) end
Correct.
Lua
public static void main(String[] args) {{}}
public static void main(String[] args) {{}}
Correct.
Java
function bar() {{ return {{key:'test'}} }}
function bar() {{ return {{key:'test'}}; }}
Return object on same line.
JavaScript
while read line; do echo $line; done < input.csv
while read line; do echo $line; done < input.csv
Correct.
Shell
def render(num): return num + 1
def render(num): return num + 1
Correct.
Python
[62, 25, 26
[62, 25, 26]
Close bracket.
Ruby